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:公式明白了,为什么还是WA?请高手指教In Reply To:公式明白了,为什么还是WA?请高手指教 Posted by:xingkongnqb at 2009-05-11 14:20:19 > #include<cstdio> > #include<algorithm> > using namespace std; > const int N=10005; > int a[10005]; > int main() > { > int N,i; > __int64 sum=0; > scanf("%d",&N); > for(i=0;i<N;i++) > scanf("%d",&a[i]); > sort(a, a+N); > for(i=0;i<N-1;i++) > { > sum+=(a[i+1]-a[i])*(i+1)*(N-i-1)*2; > /* > 从左向右传和从右向左传是对称的, 所以vol×2; > 考虑中间的一小段距离,因为左边的牛的声音 > 要传到右边的牛的耳朵里,必然要经过一些相邻的小段距离, > 这些距离是要被重复计算的,而某一段左边有i+1头牛, > 右边有n-i-1头牛,所以是(i+1)×(n-i-1) > */ > } > printf("%I64d\n",sum); > return 0; > } 在计算(a[i+1]-a[i])*(i+1)*(N-i-1)*2的时候产生了溢出,可以将前一项强制转换成(__int64) Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator