数据范围
30%的测试数据, n<=1000.
100%的测试数据, n<=1000000.
ai>=0, 保证ai在longint/int范围内, ai的总和在int64/long long范围内.
题解:
这是三倍经验啊。。。
hzwer
1 var i,n:longint; 2 x1,tot,ave,ans:int64; 3 a,c:array[0..1600000] of longint; 4 procedure sort(h,l:longint); 5 var i,j,m,temp:longint; 6 begin 7 i:=h;j:=l;m:=c[(i+j)>>1]; 8 repeat 9 while (c[i]<m) do inc(i); 10 while (c[j]>m) do dec(j); 11 if i<=j then 12 begin 13 temp:=c[i];c[i]:=c[j];c[j]:=temp; 14 inc(i);dec(j); 15 end; 16 until i>j ; 17 if i<l then sort(i,l); 18 if j>h then sort(h,j); 19 end; 20 procedure main; 21 begin 22 readln(n); 23 tot:=0; 24 for i:=1 to n do 25 begin 26 readln(a[i]);inc(tot,a[i]); 27 end; 28 ave:=tot div n; 29 c[0]:=0; 30 for i:=1 to n-1 do c[i]:=c[i-1]+a[i]-ave; 31 sort(0,n-1); 32 ans:=0;x1:=c[n>>1]; 33 for i:=0 to n-1 do inc(ans,abs(x1-c[i])); 34 writeln(ans); 35 end; 36 begin 37 main; 38 end. 39
原文:http://www.cnblogs.com/zyfzyf/p/3906116.html