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题,离散化和树状数 ...
随机推荐
- linux部署mongodb及基本操作
原文:http://blog.csdn.net/jinzhencs/article/details/50930877 一.安装部署mongo 1.创建文件夹 /opt/mongodb/single / ...
- Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 常用渗透性测试工具(Tools for penetration testing)
常用渗透性测试工具 原文:http://hi.baidu.com/limpid/item/14a2df166adfa8cb38cb3068 对一个应用项目进行渗透性测试一般要经过三个步骤. 第一步, ...
- ADT下载地址整理
參考以下文章 http://developer.android.com/tools/sdk/eclipse-adt.html 整理了官网的下载地址 http://dl.google.com/andro ...
- Android获取屏幕分辨率及DisplayMetrics简介
Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题. 手机的分辨率信息是手机的一项重要信息,很好的是,Android ...
- css伪类选择器详细解析及案例使用-----伪类选择器(2)
结构伪类选择器: <div> <ul> /*ul:only-of-type*/ <li>one</li> /*li:first-child li:nth ...
- Kinect研究
1. 深度照相机 红外 + 2个摄像头. (1彩色摄像头,1红外发射,1接收) 红外捕捉深度. 2. OpenNI 某编程社区创建. 提供基本深度数据的访问.LGPL协议. 用户跟踪付费. 3. 问题 ...
- MySQL常用命令大全(转)
下面是我们经常会用到且非常有用的MySQL命令.下面你看到#表示在Unix命令行下执行命令,看到mysql>表示当前已经登录MySQL服务器,是在mysql客户端执行mysql命令. 登录MyS ...
- (一)HTML5 - pushState 无刷新更新地址
可以解决什么问题: 可以实现网页的ajax加载,同时又能完成URL的改变而没有网页跳转刷新的迹象,就像是改变了网页的hash(#)一样. 优于hash: 避免了改变hash的问题,避免了用户不理解UR ...
- 关于R文件丢失的一个问题
android studio在编辑布局文件时,一般为了省事,如TextView控件中的text属性这样写 android:text="<500",编译不会报错,但是运行时会出 ...