正文

烦人的电梯2007-05-25 22:37:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/26140.html

分享到:

Dissatisfying Lift 
Problem 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
 
Judge Tips
Dynamic Programming

 
Problem Source
POJ Monthly

 
//我晕,题目上写的DP给害惨了,怎么都不知道阶段划分
//到网上搜了一下,别人用的是暴搜,我也用暴搜索
//没想到还是first,真是搞笑,所有不要让题目给迷惑

 


#include <stdio.h>

int main(){
 __int64 up[10002],down[10002],min,sum,ptemp;
 int     n,m,a,b,i,floor,p[10002];

 scanf("%d",&n);
 for(; n ; n--){
  scanf("%d %d %d",&m,&a,&b);
  for(i = 0; i < m; i++)
   scanf("%d",&p[i]);
  down[0] = 0;
  sum = 0;
  ptemp = p[0];
  for(i = 1; i < m; i++){
   down[i] = down[i-1] + b*ptemp + sum;
   sum += ptemp;
   ptemp += p[i];
  }
  sum = 0;
  up[m-1] = 0;
  ptemp = p[m-1];
  for(i = m-2; i >= 0; i--){
   up[i] = up[i+1] + a*ptemp + sum;
   sum += ptemp;
   ptemp += p[i];
  }
  min = 0x7fffffffffffffff;
  for(i = 0; i < m; i++){
   if(min > (down[i]+up[i]) ){
    min = down[i]+up[i];
    floor = i;
   }
  } 
  printf("%d\n",floor+1);
 }
 return 0;
}

阅读(2558) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册