Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:不知道为什么排了序不行,暴搜反而可以...In Reply To:不知道为什么排了序不行,暴搜反而可以... Posted by:yuanyirui at 2007-03-30 00:10:43 > #include <stdio.h> > #include <iostream> > using namespace std; > int main() > { > // freopen("in.txt","r",stdin); > // freopen("out.txt","w",stdout); > int a[10001],i,j,n; > long long ans; > while(scanf("%d",&n)!=EOF){ > for(i=0;i<n;i++) scanf("%d",&a[i]); > ans=0; > sort(a,a+n); > for(i=0;i<n-1;i++) ans += (a[i+1]-a[i])*(i+1)*(n-i-1); > cout<<ans*2<<endl; > } > return 1; > } > 不行 > #include <stdio.h> > #include <iostream.h> > int main() > { > // freopen("in.txt","r",stdin); > // freopen("out.txt","w",stdout); > int a[10001],i,j,n; > long long ans; > while(scanf("%d",&n)!=EOF){ > for(i=0;i<n;i++) scanf("%d",&a[i]); > ans=0; > for(i=0;i<n;i++){ > for(j=i+1;j<n;j++){ > ans+=abs(a[i]-a[j]); > } > } > cout<<ans*2<<endl; > } > return 1; > } > 反而可以过 这一句int可能溢出ans += (a[i+1]-a[i])*(i+1)*(n-i-1); 改为ans += (long long )(a[i+1]-a[i])*(i+1)*(n-i-1);即可 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator