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 ;
}

CDOJ 1225 Game Rooms的更多相关文章

  1. Ural 1225. Flags 斐波那契DP

    1225. Flags Time limit: 1.0 secondMemory limit: 64 MB On the Day of the Flag of Russia a shop-owner ...

  2. [LeetCode] Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  3. [LeetCode] Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  4. cdoj 1489 老司机采花

    地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others)     M ...

  5. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

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

  7. 【BZOJ】1225: [HNOI2001] 求正整数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1225 题意:给一个数n,求一个最小的有n个约数的正整数.(n<=50000) #include ...

  8. [SmartFoxServer概述]Zones和Rooms结构

    Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...

  9. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

随机推荐

  1. 逆序数 HDOJ 4911 Inversion

    题目传送门 题意:可以交换两个相邻的数字顺序k次,问最后逆序对最少有多少 分析:根据逆序数的定理如果逆序数大于0,那么必定存在1<=i<n使得i和i+1交换后逆序数减1假设原逆序数为cnt ...

  2. 关于itchat用法的一篇博文

    itchat的原理就是利用爬虫爬取了网页版微信的内容,并进行一系列的操作,运用微信,通过手机与电脑时登录的互通性,可以实现用微信对电脑的操作,通过itchat.msg_register方法,可以得到目 ...

  3. R语言中的并行处理

    网上有人说foreach包可以并行,我就去弄了,结果发现一个普通的二重循环什么事都不错都很卡!捣鼓了半天才发现是foreach的问题 为了提速,做了如下事宜: 直接利用矩阵列加减,不是一个个遍历加 把 ...

  4. 初学.net增删改查

    分页显示 DAL: public List GetListByPager(int PageIndex, int PageSize, out int RowCount) { string sql = & ...

  5. 猩球StarBall ,一个方便约球的小程序

    扫描小程序码直接进入小程序 猩球StarBall 是一款为热爱运动的人群提供便利的小程序. 开发技术为Java +Mysql 其中用到的技术框架为SpringBoot,Mybatis,Redis,Qu ...

  6. JD IPO address by liuqiangdong

    Ladies and gentlemen, Good evening.I'd rather use english, not mandarin.Because during the road show ...

  7. Linux系统结构与终端控制台

    Linux系统结构与终端控制台 作者:Vashon 时间:20150418 以下主要是对Linux系统终端控制台切换及基本操作的范例,其他的理论就不多说了,直接进入实践部分. Starting.... ...

  8. vue项目中常用的一些公共方法

    //校验手机号码 export function isSpecialPhone(num) { return /^1[2,3,4,5,7,8]\d{9}$/.test(num) } //校验中英文姓名 ...

  9. 原创:shell两个整数的比较 思想版

    思想是学的 代码创作是自己的 很喜欢前几行的逻辑严谨 #!/bin/bash#判断两个整数的大小read -p "请输入两个整数a b :" a b #或者使用a=$1[ -z & ...

  10. uva1609 Foul Play

    思维 创造条件使一轮比赛之后仍满足1号打败至少一半,并剩下至少一个t' 紫书上的思路很清晰阶段1,3保证黑色至少消灭1半 #include<cstdio> #include<vect ...