1 /* 2 Every tenant went up N floors would make the dissatisfied degree rise N * a + 0.5 * N * (N - 1) degrees, 3 every tenant went down N floors would make the dissatisfied degree rise N * b + 0.5 * N * (N - 1) degrees. 4 5 △= Dp+1 – Dp 6 = ∑1=<i<=p Ki*(b+p-i)) - ∑p<i<=n Ki*(a+i-p-1) 7 = ∑1=<i<=n Ki * i +(b+p)*∑1=<i<=p Ki - (a-p-1)∑p<i<=n Ki 8 */ 9 10 #include <iostream> 11 #define MAXN 10001 12 using namespace std; 13 14 __int64 _m[MAXN+1]; 15 int k[MAXN+1]; 16 __int64 p_2[MAXN+1]; 17 __int64 p_3[MAXN+1]; 18 int k_1[MAXN]; 19 int k_2[MAXN]; 20 21 22 int main() 23 { 24 //freopen("acm.acm","r",stdin); 25 int test; 26 int n; 27 int i; 28 int j; 29 int p_1; 30 int a; 31 int b; 32 scanf("%d",&test); 33 while(test --) 34 { 35 scanf("%d",&n); 36 scanf("%d",&a); 37 scanf("%d",&b); 38 39 for(i = 1; i <= n; ++ i) 40 { 41 scanf("%d",&k[i]); 42 } 43 44 for(i = 1; i <= n; ++ i) 45 { 46 k_1[i] = k_2[i] = k[i]; 47 } 48 49 for(i = n-1; i >=1; -- i) 50 { 51 k_2[i] += k_2[i+1]; 52 } 53 54 for(i = 2; i <= n; ++i) 55 { 56 k_1[i] += k_1[i-1]; 57 } 58 59 p_1 = 0; 60 for(i = 1; i <= n; ++ i) 61 { 62 p_1 += k[i]*i; 63 } 64 65 for(i = 1; i <= n; ++ i) 66 { 67 p_2[i] = (b + i)*k_1[i]; 68 } 69 70 for(i = 1; i <= n; ++ i) 71 { 72 k_2[i] -= k[i]; 73 } 74 75 76 copy(k_2,k_2+n+1,p_3); 77 78 for(i = 1; i <= n; ++ i) 79 { 80 p_3[i] *= (a-i-1); 81 } 82 _m[1] = 0; 83 for(i = 2; i <= n; ++ i) 84 { 85 _m[1] += (i-1)*a+((i-1)*(i-2)/2); 86 } 87 88 for(i = 2; i <= n; ++ i) 89 { 90 _m[i] = _m[i-1] - p_1 + p_2[i-1] - p_3[i-1]; 91 } 92 93 __int64 min = 999999999999999; 94 95 int place; 96 97 for(i = 1; i <= n; ++ i) 98 { 99 if(_m[i] < min) 100 { 101 min = _m[i]; 102 place = i; 103 } 104 } 105 cout<<place<<endl; 106 } 107 }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com