The 2015 China Collegiate Programming Contest Game Rooms
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≤T≤100). T test cases follow. Each test case begins with one line with an integer N(2≤N≤4000), the number of floors in the building. N lines follow, each consists of 2 integers, Ti and Pi(1≤Ti,Pi≤109), 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 and output
| Sample Input | Sample Output |
|---|---|
1 |
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 4table tennis players on the second floor will need to go one floor down. So the total distance is 9.
Source
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = + ;
long long dp[][maxn][] , cnt[][maxn][] , sum[maxn][];
int val[maxn][] , cur , n ; void updata(long long & x , long long v){
if(x==-) x = v;
else x = min( x , v );
} int main(int argc,char *argv[]){
int Case;
scanf("%d",&Case);
for(int cas = ; cas <= Case ; ++ cas){
cur = ; memset(dp[cur] , - , sizeof(dp[cur])) ; memset(cnt[cur] , ,sizeof(cnt[cur]));
scanf("%d",&n);
for(int i = ; i <= n ; ++ i){
scanf("%d%d",&val[i][] , &val[i][]);
for(int j = ; j < ; ++ j) sum[i][j] = sum[i-][j]+1LL*val[i][j];
}
cnt[cur][][] = val[][] , cnt[cur][][] = val[][] , dp[cur][][] = dp[cur][][] = ;
for(int i = ; i <= n ; ++ i){
int pre = cur ; cur ^= ; memset(dp[cur] , - , sizeof(dp[cur])); memset(cnt[cur] , ,sizeof(cnt[cur]));
for(int j = ; j < ; ++ j) cnt[cur][i][j]=val[i][j];
for(int j = ; j < i ; ++ j)
for(int k = ; k < ; ++ k){
cnt[cur][j][k] = cnt[pre][j][k] + sum[i][k] - sum[j-][k];
if(~dp[pre][j][k]){
if(j==){
updata( dp[cur][j][k],dp[pre][j][k]);
updata( dp[cur][i][k^] , dp[pre][j][k] + cnt[pre][][k^] + val[i][k]);
}
else{
long long extra = ;
if(((i+j)&)==) extra = val[(i+j)>>][k^]*1LL*(((i-j)>>)+);
updata( dp[cur][j][k] , dp[pre][j][k] + extra);
updata( dp[cur][i][k^] , dp[pre][j][k] + cnt[pre][(i+j+)>>][k^] + val[i][k]);
}
}
}
}
long long ans = min( dp[cur][n][] , dp[cur][n][]);
for(int i = ; i < n ; ++ i)
{
for(int k = ; k < ; ++ k){
long long add = ;
int end = (n + i + ) >> ;
for(int j = n ; j >= end ; -- j){
add += val[j][k^] * 1LL* (j - i + );
}
ans = min( ans , dp[cur][i][k] + add);
}
}
printf("Case #%d: %lld\n",cas,ans);
}
return ;
}
The 2015 China Collegiate Programming Contest Game Rooms的更多相关文章
- The 2015 China Collegiate Programming Contest A. Secrete Master Plan hdu5540
Secrete Master Plan Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- The 2015 China Collegiate Programming Contest K Game Rooms hdu 5550
Game Rooms Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- The 2015 China Collegiate Programming Contest L. Huatuo's Medicine hdu 5551
Huatuo's Medicine Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547
Sudoku Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- The 2015 China Collegiate Programming Contest G. Ancient Go hdu 5546
Ancient Go Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- The 2015 China Collegiate Programming Contest E. Ba Gua Zhen hdu 5544
Ba Gua Zhen Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543
Pick The Sticks Time Limit: 15000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)
当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...
随机推荐
- java 枚举类型
原来枚举类型还可以这样玩... public enum Tenum { None(1),ByteArray(2),List(3),Map(4); private int id; private Ten ...
- CSS常用选择器
关于CSS常用选择器: 1.ID选择器 关于ID选择器具有唯一性,在文档流中,ID是唯一的,在低版本的浏览器中,允许出现不适唯一ID的情况,而在高版本的浏览器中,出现ID不唯一的情况浏览器会出现的报错 ...
- KVM硬件辅助虚拟化之 EPT(Extended Page Table)
传统OS环境中,CPU对内存的訪问都必须通过MMU将虚拟地址VA转换为物理地址PA从而得到真正的Physical Memory Access,即:VA->MMU->PA,见下图. 虚拟执行 ...
- HDU 5067-Harry And Dig Machine(DFS)
Harry And Dig Machine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- zip命令的使用
zip命令可以用来将文件压缩成为常用的zip格式.unzip命令则用来解压缩zip文件. 1. 我想把一个文件abc.txt和一个目录dir1压缩成为yasuo.zip: # zip -r yasuo ...
- MYSQL存储过程注释位置
MYSQL数据库存储过程,像"#“和”/%%/“注释需要写在BEGIN,END语句块里面,否则保存不了,例如: CREATE PROCEDURE HelloWorld() BEGIN #大家 ...
- Spring框架知识总结-注入Bean的各类异常
近日整合sping和hibernate框架时遇到了一系列的异常,本次主要说明一下spring框架可能出现的异常及解决方案. 我们借助sping强大的bean容器管理机制,通过BeanFactory轻松 ...
- 现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数,读取N条记录输入,再按要求输出。 N<100
#include <iostream> using namespace std; struct student {char num[100]; char name[100]; int ...
- 类和对象:一些相关的BIF - 零基础入门学习Python040
类和对象:一些相关的BIF 让编程改变世界 Change the world by program 一些类和对象相关的 BIF 今天我们来谈谈跟类和对象相关的一些BIF(内置函数): issubcla ...
- 解决ie6显示透明图的问题
在我们设置png透明图片时,其他浏览器都显示很正常,唯独只有ie6看着不是透明的状态. 第一种办法是:单独设置ie6的样式.例: _background: none; _filter:progid:D ...