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. SPRING-BOOT系列之SpringBoot的诞生及其和微服务的关系

    转载自 : https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法 ...

  2. position 位置、表单

    一.position    位置 1.只要使用了定位,必须有一个相对的参照物 2.具体定位的那个元素需加position:absolute:(绝对的)   绝对的:就是具体到某一个地方,特别详细的意思 ...

  3. 一份最贴近真实面试的Java基础面试题

    这是一份Java基础知识的面试题.在网上的关于Java的面试题数不胜数,但认真看过感觉大多数都没有实用性,有很多是面试官根本就不会问到的,那些已经脱离了实际开发的技术问题.而这份资料来源自一份个人觉得 ...

  4. 初识Vivado

    Vivado 设计套件包括高度集成的设计环境和新一代从系统到 IC 级的工具,这些均建立在共享的可扩展数据模型和通用调试环境基础上.这也是一个基于 AMBA AXI4 互联规范.IP-XACT IP ...

  5. mac自带终端安装完ohmyZsh后显示乱码

    修改描述文件-添加 选择新导入的 Meslo LG M Regular for Powerline

  6. 实现流水灯以间隔500ms的时间闪烁(系统定时器SysTick实现的精确延时)

    /** ****************************************************************************** * @file main.c * ...

  7. 洛谷 P1548 棋盘问题

    题目描述 设有一个N*M方格的棋盘(l<=N<=100,1<=M<=100)(30%) 求出该棋盘中包含有多少个正方形.多少个长方形(不包括正方形). 例如:当 N=2, M= ...

  8. CPP-基础:模板

    // template.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> #include &l ...

  9. CPP-基础:inline

    背景: 在C&C++中 一.inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例: #define ExpressionNam ...

  10. spring-3-AOP

    自定义注解类 1.定义注解类 package anno; import java.lang.annotation.ElementType; import java.lang.annotation.Re ...