CDOJ 1225 Game Rooms
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
#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的更多相关文章
- 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 ...
- [LeetCode] Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- cdoj 1489 老司机采花
地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others) M ...
- codeforces 519E A and B and Lecture Rooms LCA倍增
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- 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 ...
- 【BZOJ】1225: [HNOI2001] 求正整数
http://www.lydsy.com/JudgeOnline/problem.php?id=1225 题意:给一个数n,求一个最小的有n个约数的正整数.(n<=50000) #include ...
- [SmartFoxServer概述]Zones和Rooms结构
Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
随机推荐
- Hu矩
close all; clear all; I1=imread('lena.bmp'); angle=; T=[cos(angle),sin(angle),;-sin(angle),cos(angle ...
- Problem 2238 Daxia & Wzc's problem 1627 瞬间移动
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1627 http://acm.fzu.edu.cn/problem.php ...
- P1979 华容道 spfa题解
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
- 一些关于Spring的随笔
Spring的IOC.AOP IOC(Inversion of Control): spring容器控制了所有的bean,不用spring以前,一个bean要依赖另一个bean就在这个bean里初始化 ...
- CF749C Voting
题目链接: http://codeforces.com/problemset/problem/749/C 题目大意: 共有n个人,编号为1~n.他们每个人属于且仅属于R阵营或N阵营中的一个.现在他们要 ...
- checkbox:click事件触发文本框显示隐藏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- web前端怎么样才能入门
web前端怎么样才能入门,首先我们要从什么是初级web前端工程师说起: 按照我的想法,我把前端工程师分为了入门.初级.中级.高级这四个级别: 入门级别指的是了解什么是前端(前端到底是什么其实很多人还是 ...
- Azure ARMTemplate模板,VM扩展命令
Azure ARM模板中,给虚拟机安装扩展脚本的命令 "resources": [ { "apiVersion": "[variables('apiV ...
- UVALive 2238 Fixed Partition Memory Management 固定分区内存管理(KM算法,变形)
题意:目前有一部分可用内存,分为m个大小固定的区域.现有n个程序要执行,每个程序在不同大小的内存中运行所需的时间不同,要使运行完所有程序所耗时最少,问每个程序在哪块区域中从什么时间运行到什么时间,以及 ...
- Jenkins执行sudo权限的设置
Jenkins系统中添加执行脚本的时候,有一些命令是需要sudo权限和来执行的,可以在root权限下添加一下Jenkins账号的权限 1.添加不需要密码可sudo执行指定命令的权限 cd /etc c ...