poj 1701 Dissatisfying Lift

Dissatisfying Lift

Time Limit: 1000MS
Memory Limit:10000K

Total Submissions: 4721
Accepted:1013

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
 
#include <iostream>
using namespace std;

__int64 num[10005], up[10005], down[10005], f[10005];

int main(){
    int t, m, a, b, sum0, sum1, ans;
    int i;
    cin>>t;
    while(t--){
        cin>>m>>a>>b;
        for (i = 1; i <= m; i++) scanf("%l64d",&num[i]);
        sum0 = num[1]; sum1 = 0; up[1] = 0;
        for (i = 2; i <= m; i++){
            up[i] = up[i - 1] + b * sum0 + sum1;
            sum1 += sum0; sum0 += num[i];
        }
        sum0 = num[m]; sum1 = 0; down[m] = 0;
        for (i = m - 1; i >= 1; i--){
            down[i] = down[i + 1] + a * sum0 + sum1;
            sum1 += sum0; sum0 += num[i];
        }
        ans = 1;
        for (i = 1; i <= m; i++){
            num[i] = up[i] + down[i];
            if (num[ans] > up[i] + down[i]) ans = i;
        }
        cout<<ans<<endl;
    }
    return 0;
}
posted @ 2011-11-21 22:33  w0w0  阅读(287)  评论(0编辑  收藏  举报