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 5090 Game with Pearls

    题目传送门 /* 题意:给n, k,然后允许给某一个数加上k的正整数倍,当然可以不加, 问你是否可以把这n个数变成1,2,3,...,n, 可以就输出Jerry, 否则输出Tom. 贪心:保存可能变成 ...

  2. 题解报告:hdu 6441 Find Integer(费马大定理+智慧数)

    Problem Description people in USSS love math very much, and there is a famous math problem .give you ...

  3. solr管理界面下统计多个时间段的数据 facet.query

    在Raw Query Parameters参数里面输入时间段即可 如下图所示: facet.query=publishTime:[2017-06-05T00:00:00Z TO 2017-06-07T ...

  4. AJPFX关于抽象类和接口的区别

    一.设计目的不同:接口体现的是一种规范,,类似于系统的总纲,它制定了系统的各模块应遵守的标准抽象类作为多个子类的共同父类,体现的是模式化的设计,抽象类可以认为是系统的中间产品,已经实现了部分功能,部分 ...

  5. 使用cordova把h5应用打包成apk

    由于h5应用开发不是本例重点,因此直接提供一个最简单的h5应用代码,此应用使用vue-cli框架开发 此h5应用叫vue1,用webstrom打开vue1,进行npm install安装引用 vue1 ...

  6. OCP 11g 第四章练习

    练习 4-1 配置Oracle Net 在本练习中, 将使用图形化工具和命令行工具来建立一个完整的Oracle Net 环境. 由此, 读者可看出在Windows 和 Linux 系统中的区别. 1. ...

  7. sprintf使用时需要注意的问题

  8. How `delete’ works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=21 February 16, 2013 How `delete’ works ...

  9. java将一个List赋值给另一个List的4种方法

    编辑 删除 声明:ArrayList a, 仅仅只是声明了一个list变量,其未来作用相当于C++中的引用变量,亦或者相当于一个对象块的索引,但并未为其分配具体的完整的对象所需要的内存空间,其所分配的 ...

  10. 最新精品 强势来袭 XP,32/64位Win7,32/64位Win8,32/64位Win10系统【国庆版版】

    本系统是10月最新完整版本的Windows10 安装版镜像,Win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为Win10 Edge浏览器中国默认主页和搜索引擎,系 ...