bzoj 1045

 

确定初始状态(n与1直接谁给了谁几个),后面的就确定了,再根据总结出来的东西决定前面谁给谁几个最优。

n=1000000!!!

 1 /**************************************************************
 2     Problem: 1045
 3     User: idy002
 4     Language: C++
 5     Result: Accepted
 6     Time:1852 ms
 7     Memory:12524 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <algorithm>
12 #define maxn 1000010
13 using namespace std;
14  
15 int n;
16 int a[maxn]; 
17 long long s[maxn], sum, mv;
18  
19 int main() {
20     scanf( "%d", &n );
21     for( int i=1; i<=n; i++ ) {
22         scanf( "%d", a+i );
23         sum += a[i];
24     }
25     sum /= n;
26     for( int i=1; i<=n; i++ ) 
27         a[i] -= sum;
28     for( int i=1; i<=n; i++ ) 
29         s[i] = s[i-1]+a[i];
30     int mid = (1+n)>>1;
31     nth_element( s+1, s+mid, s+1+n );
32     mv = s[mid];
33     long long ans = 0, ta;
34     for( int i=1; i<=n; i++ ) {
35         ta = s[i]-mv;
36         if( ta<0 ) ta=-ta;
37         ans += ta;
38     }
39     printf( "%lld\n", ans );
40 }
View Code

 

posted @ 2015-02-25 21:46  idy002  阅读(145)  评论(0编辑  收藏  举报