Dissatisfying Lift
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5654 | Accepted: 1282 |
Description
There's a building with M floors. The amounts of tenants of every floor are K1, K2, K3, ..., Km. One day all the tenants went home together and they took the same lift (suppose the lift was large enough). Because of some reason the lift could only stop on one floor and the tenants must go upstairs or downstairs to their houses. Every tenant went up N floors would make the dissatisfied degree rise N * a + 0.5 * N * (N - 1) degrees, and every tenant went down N floors would make the dissatisfied degree rise N * b + 0.5 * N * (N - 1) degrees. Your task is to tell which floor the lift should stop, in order to make the dissatisfied degree as low as possible.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each test contains M (1 <= M <= 10000), a and b (0 <= a, b <= 100). The second line contains K1, K2, K3, ..., Km (0 <= Ki <= 20, i = 1..M).
Output
For each test case, print a line containing a single integer, indicating which floor the lift should stop.
Sample Input
1
5 3 2
1 1 1 1 1
Sample Output
3
积累词汇:
tenant | 英 [ˈtenənt] 美 [ˈtɛnənt] | n.房客; 佃户;vt.租借,租用; |
注意数据类型,不满意度要用long long。
推导公式如下:
上楼不满意度类似。
AC代码:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main() 5 { 6 int T; 7 cin>>T; 8 while(T--) 9 { 10 int M,a,b,i; 11 int k[10001]; 12 long long down[10001],up[10001]; 13 cin>>M>>a>>b; 14 for(i=1;i<=M;i++) cin>>k[i]; 15 long long sum0=k[1],sum1=0; 16 17 down[1]=0; 18 for(i=2;i<=M;i++)//求下楼不满意度 19 { 20 down[i]=down[i-1]+b*sum0+sum1; 21 sum1+=sum0;//这条语句和下面语句位置不能换 22 sum0+=k[i]; 23 } 24 25 sum0=k[M];sum1=0; 26 up[M]=0; 27 for(i=M-1;i>=1;i--)//求上楼不满意度 28 { 29 up[i]=up[i+1]+a*sum0+sum1; 30 sum1+=sum0; 31 sum0+=k[i]; 32 } 33 34 long long min_d=down[1]+up[1],d=1; 35 for(i=2;i<=M;i++) 36 { 37 if(down[i]+up[i]<min_d) 38 { 39 min_d=down[i]+up[i];//最小不满意度 40 d=i;//楼层 41 } 42 } 43 cout<<d<<endl; 44 } 45 } 46