Game Rooms

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 305    Accepted Submission(s): 84

Problem Description
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
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.

 
题意:有n层楼,每层楼有人想要打乒乓球或者羽毛球。
每层楼可以改造成一种场地(羽毛球场或者乒乓球场),容量无限大。
每个人可以上下楼。
问,这些人都进行自己想要进行的运动总共要走多少楼。
分析:显然,每个人都会选择离自己最近的场地。
有一个显然的dp
dp[i][0..1]表示前i层,第i层建造羽毛球场或者乒乓球场(0..1)的答案。
但是直接这样做的话会有后效性。
因为如果i与i+1种类不一样的话,i之前的某一段可能会选择向上走,而不是向下走。
为此,我们默认dp[i][0..1]得到答案是默认i与i+1不同的答案。
转移枚举一个j,表示j+1到i都建造一样的,而j和i+1都与它们不一样,如此,j+1到i这一段的人(需要转移场地的),必定一半向上一半向下。
这个代价是可通过预处理以O(1)算的的。
 
问题来了,这个dp还是O(n^2)的。
 
但是我不会优化了。。。比赛快要截止时,试了一发。。。居然AC了。。。
 #include <cstdio>
#include <iostream>
using namespace std; const int N = ;
typedef long long LL;
const LL MLL = 1000000000000000001LL;
int n, arr[N][];
LL distDown[N][], distUp[N][], sum[N][], dp[N][], ans; inline LL Up(int l, int r, bool s)
{
if(l > r) return ;
return (distUp[l][s ^ ] - distUp[r + ][s ^ ]) -
(sum[r][s ^ ] - sum[l - ][s ^ ]) * (n - r);
} inline LL Down(int l, int r, bool s)
{
if(l > r) return ;
return (distDown[r][s ^ ] - distDown[l - ][s ^ ]) -
(sum[r][s ^ ] - sum[l - ][s ^ ]) * (l - );
} inline LL Calc(int l, int r, bool state)
{
if(l == )
{
return Up(l, r, state);
}
int mid = (l + r) >> ;
return Up(mid + , r, state) + Down(l, mid, state);
} inline void Solve()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
for(int j = ; j < ; j++) scanf("%d", &arr[i][j]); for(int i = ; i < ; i++)
{
distDown[][i] = ;
for(int j = ; j <= n; j++)
{
distDown[j][i] = distDown[j - ][i] + 1LL * arr[j][i] * j;
sum[j][i] = sum[j - ][i] + arr[j][i];
}
distUp[n + ][i] = ;
for(int j = n; j >= ; j--)
distUp[j][i] = distUp[j + ][i] + 1LL * arr[j][i] * (n + - j);
} ans = MLL;
dp[][] = dp[][] = ;
for(int i = ; i < n; i++)
{
dp[i][] = dp[i][] = MLL;
for(int j = ; j < i; j++)
{
dp[i][] = min(dp[i][], dp[j][] + Calc(j + , i, ));
dp[i][] = min(dp[i][], dp[j][] + Calc(j + , i, ));
} ans = min(ans, dp[i][] + Down(i + , n, ));
ans = min(ans, dp[i][] + Down(i + , n, ));
}
cout << ans << endl;
} int main()
{
int test;
scanf("%d", &test);
for(int testnumber = ; testnumber <= test; testnumber++)
{
printf("Case #%d: ", testnumber);
Solve();
}
return ;
}
 

The 2015 China Collegiate Programming Contest K Game Rooms hdu 5550的更多相关文章

  1. 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 ...

  2. 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 ...

  3. The 2015 China Collegiate Programming Contest Game Rooms

    Game Rooms Time Limit: 4000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)

    当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...

随机推荐

  1. MyString(重写String)

    http://wenku.baidu.com/view/d7ac113243323968011c925b.html 已知类String的原型为: class String  { public:     ...

  2. NYOJ题目97兄弟郊游问题

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr8AAAHxCAIAAADrwUM4AAAgAElEQVR4nO3dLXLjytfH8f8mzLOQYC

  3. Eclipse的使用及Java程序的标识符和关键字

    Eclipse的使用 (1)创建Java项目 选择“文件”/“新建”/“Java项目”命令,在弹出的“新建Java项目”对话框输入项目名,然后点击“下一步”,最后单击“完成”. (2)创建Java类文 ...

  4. 注意kvm在安装虚机的时候不能把存放虚机的文件放在/root 下面

    如果放在/root下面会报错: WARNING KVM acceleration not available, using 'qemu' Starting install... Creating st ...

  5. Deci and Centi Seconds parsing in java

    http://stackoverflow.com/questions/14558663/deci-and-centi-seconds-parsing-in-java

  6. map find 是线程安全的吗

    测试环境gcc4.8.2     iterator find ( const key_type& k ); const_iterator find ( const key_type& ...

  7. HTML5_Canvas_属性、定义及方法

    一.简单图形,整套的属性和方法专门用于绘制矩形:1.fillStyle可以设置为CSS颜色.一个图案或一种颜色渐变.fillStyle默认是纯黑色,你可以设置成你喜欢的任意颜色.只要页面打开着,每个绘 ...

  8. C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏

    using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...

  9. PHP利用jquery生成各种验证码和Ajax验证

    PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP 生成验证码的大致流程有: .产生一张png的图片: .为图片设置背景 ...

  10. 不通过App Store实现ios应用分发下载安装

    最近公司的项目准备着手宣传工作了,宣传手册上要印制App的下载地址二维码,但是客户端应用还未上线,需要一种临时的方案解决应用分发下载问题,通常ios应用必须通过苹果应用商店才能下载安装,但是也可以看到 ...