Game Rooms

Time Limit: 4000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

Your company has just constructed a new skyscraper, but you just noticed a terrible problem: there is only space to put one game room on each floor! The game rooms have not been furnished yet, so you can still decide which ones should be for table tennis and which ones should be for pool. There must be at least one game room of each type in the building.

Luckily, you know who will work where in this building (everyone has picked out offices). You know that there will be Ti table tennis players and Pi pool players on each floor. Our goal is to minimize the sum of distances for each employee to their nearest game room. The distance is the difference in floor numbers: 0 if an employee is on the same floor as a game room of their desired type, 1 if the nearest game room of the desired type is exactly one floor above or below the employee, and so on.

Input
The first line of the input gives the number of test cases, $T(1\leq T\leq 100)$. T test cases follow. Each test case begins with one line with an integer $N(2\leq N\leq 4000)$, the number of floors in the building. N lines follow, each consists of 2 integers, $Ti and Pi(1\leq T_i,P_i\leq 10^9)$, the number of table tennis and pool players on the ith floor. The lines are given in increasing order of floor number, starting with floor 1 and going upward.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimal sum of distances.

Sample Input

1
2
10 5
4 3

Sample Output

Case #1: 9

Hint
In the first case, you can build a table tennis game room on the first floor and a pool game room on the second floor. In this case, the 5 pool players on the first floor will need to go one floor up, and the 4 table tennis players on the second floor will need to go one floor down. So the total distance is 9.

Source

The 2015 China Collegiate Programming Contest
 
解题:动态规划大法真是无敌
dp[i][0]表示第i层放0,第i+1层放1,所以前面放0的层数那些玩1的人要跑到i+1去玩1了,这就是dp的初始化
那么转移 dp[i][0] 可以由 dp[j][1]转移到来,因为j层放1,i+1层放1,故j+1到i层玩1的人有两个去处,均摊给j和i+1两个楼层较优
这就是转移
那么最后由于dp[i][0],i层放的是0并且i+1层放的是1,所以i+1层的0要到i来玩0,取最小的
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = ~0ULL>>;
const int maxn = ;
LL a[maxn][],sum[maxn][],f[maxn][],dp[maxn][];
LL S(int a,int b,int x){
return sum[b][x] - sum[a-][x];
}
LL cost(int a,int b,int x){
if(a == b) return ;
if(a < b) return S(a,b,x)*b - f[b][x] + f[a-][x];//up
return f[a][x] - f[b-][x] - S(b,a,x)*b;//down
}
int main(){
int kase,n,cs = ;
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
for(int i = ; i <= n; ++i){
scanf("%lld%lld",&a[i][],&a[i][]);
sum[i][] = sum[i-][] + a[i][];
sum[i][] = sum[i-][] + a[i][];
f[i][] = f[i-][] + a[i][]*i;
f[i][] = f[i-][] + a[i][]*i;
}
LL ret = INF;
for(int i = ; i < n; ++i){
dp[i][] = cost(,i + ,);
dp[i][] = cost(,i + ,);
for(int j = ; j < i; ++j){
int mid = (i + j + )>>;
dp[i][] = min(dp[i][],dp[j][] + cost(mid,j,) + cost(mid + ,i + ,));
dp[i][] = min(dp[i][],dp[j][] + cost(mid,j,) + cost(mid + ,i + ,));
}
ret = min(ret,dp[i][] + cost(n,i,));
ret = min(ret,dp[i][] + cost(n,i,));
}
printf("Case #%d: %lld\n",cs++,ret);
}
return ;
}

最新文章

  1. SQL——用户定义函数
  2. event driven的一些概念
  3. wage
  4. Mysql 的MYISAM引擎拷贝出现异常——Incorrect information in file &#39;xxx.frm&#39;
  5. HDU 1241 Oil Deposits (DFS/BFS)
  6. Android音频底层调试-基于tinyalsa
  7. Oracle - 找不到原因的无效字符
  8. sqlserver system object type
  9. iOS-CYLTabBarController【好用的TabbarController】
  10. MongoDB基础学习
  11. JDK8 BigDecimal API-创建BigDecimal源码浅析二
  12. JS的函数节流(throttle)
  13. Linux下如何让jar包程序在后台一直执行
  14. impala系列:impala特有的操作符
  15. esp-adf Element PipeLine
  16. 解决vmware与主机无法连通的问题
  17. http方式访问svn
  18. mysqlbinlog 查看mysql bin 日志 mysqlbinlog: unknown variable &#39;default-character-set=utf8&#39;
  19. 转载,自己留着看eclipse 快捷键
  20. loj#2542. 「PKUWC2018」随机游走(MinMax容斥 期望dp)

热门文章

  1. jsp问题记录
  2. html img标签显示一个默认图片
  3. CoreData修改了数据模型报错 The model used to open the store is incompatible with the one used to create the store
  4. DOCTYPE的使用
  5. IP地址 子网掩码 默认网关和DNS服务器的关系
  6. Oracle 11g r2 Enterprise Manager (EM) 中文页面调整为英文页面
  7. Linux 从源码编译安装 Nginx
  8. js单线程和js异步操作的几种方法
  9. 如何修改站点url
  10. 【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function