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. XPath的基本使用

    XPath XPath 使用路径表达式来选取 XML 文档中的节点或节点集. 路径表达式 结果 bookstore 选取 bookstore 元素的所有子节点. /bookstore 选取根元素 bo ...

  2. python基础语法(二)

    本文主要包括以下内容 函数 切片 迭代 列表生成式 生成器 迭代器 函数 定义函数 定义函数 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块 ...

  3. ssh -v root@xxxxx 显示登录的细节

    [root@ok .ssh]# ssh -v root@10.100.2.84 OpenSSH_5.3p1, OpenSSL Feb debug1: Reading configuration dat ...

  4. Unity依赖注入使用

    构造器注入(Constructor Injection):IoC容器会智能地选择选择和调用适合的构造函数以创建依赖的对象.如果被选择的构造函数具有相应的参数,IoC容器在调用构造函数之前会自定义创建相 ...

  5. Filp Game

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25573   Accepted: 11052 题目链接: ...

  6. 学习ASP.NET缓存机制

    缓存是大型BS架构网站的性能优化通用手段,之前知道有这个概念,并且也知道很重要,但是一直没静下心来了解.这次借着学习PetShop源码的机会熟悉一下ASP.NET基本的缓存机制(生产环境中的真实缓存有 ...

  7. WIN10 新建ORACLE实例

    1 管理员身份进入CMD环境,执行DBCA命令,在弹出窗口的引导中,完成实例创建 2 如果在创建过程中没有选择适当的字符集(最好采用默认字符集),如下图所示,在进入PLSQL DEVELOPER的时候 ...

  8. freemarker 实现对URL的安全编码

    [#setting url_escaping_charset='utf-8'] ${yourstr?url}

  9. Solr入门之(4)配置文件solr.xml

    <?xml version="1.0" encoding="UTF-8" ?> <!-- This is an example of a si ...

  10. hdu 1404 找sg ***

    HDU 1404  Digital Deletions 一串由0~9组成的数字,可以进行两个操作:1.把其中一个数变为比它小的数:2.把其中一个数字0及其右边的所以数字删除. 两人轮流进行操作,最后把 ...