HDU 5550 Game Rooms (dp+前缀和预处理)
来源:互联网 发布:淘宝怎么看别人日销量 编辑:程序博客网 时间:2024/06/05 03:51
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≤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.
大致题意:一个楼一共有n层,每一层分别有ai个人喜欢台球,bi个人喜欢游泳,但每一层只能建一种设施,所以在当前层x得不到满足的人会到距离他最近的一个建有他喜欢的设施的那一层y去,路程花费即为abs(y-x+1),问你修建完后,所有人都得到满足后的最少总路程花费是多少。
思路:一开始考虑的n3的dp,看了别人的博客后才知道有这么巧妙的n2dp。。。
假设dp[2][N],dp[f][i]表示此时第i层修建f类型的设施,且第i+1层修建的是f^1类型的设施时,前i层的人最少总路程花费,然后我们枚举第i层上面离第i层最近的那个修建不同类型设施所在层 j,j>=1&& j < i,然后此时j到i这个区间内所修的设施都是f,所以此时该区间的最少花费即喜欢f^1的人到j或者i+1层的最少路程花费,那么状态转移方程即为:
dp[f][i]=min( dp[f][i],dp[f^1][i]+query( f^1,j,i ) )
因为最优解可能是最后某一段连续修的相同的设施,所以我们不能直接用这个dp值作为答案,所以每一次我们都假设此时i+1到n这个区间修的都是一样的设施,那么ans=min( ans,dp[f][i]+queryup( f,i,n ) ).
然后用前缀和预处理下,我们就能在O(1)的时间内算出某段区间到某个位置的最少路程花费。
代码如下
//#include<bits/stdc++.h>#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std; #define LL long long intconst int N=4005;LL a[N],b[N];LL dp[2][N];LL sum[2][N]; LL dsum[2][N];LL querydown(int f,int l,int r)//求l+1到r这个区间内到r+1这个位置的花费 { return (sum[f][r]-sum[f][l])*(r+1)-(dsum[f][r]-dsum[f][l]);} LL queryup(int f,int l,int r)//求l+1到r这个区间内到l这个位置的花费{ return (dsum[f][r]-dsum[f][l])-(sum[f][r]-sum[f][l])*l;} LL query(int f,int l,int r)//求l+1到r这个区间内到l或者r+1这两个位置的最少花费 { int mid=(l+r)/2; return queryup(f,l,mid)+querydown(f,mid,r);//一部分向上走,一部分向下走}int main(){ int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++) { int n; scanf("%d",&n); sum[0][0]=sum[1][0]=0; dsum[0][0]=dsum[1][0]=0; for(int i=1;i<=n;i++) { scanf("%lld%lld",&a[i],&b[i]); sum[0][i]=sum[0][i-1]+a[i]; sum[1][i]=sum[1][i-1]+b[i]; dsum[0][i]=dsum[0][i-1]+a[i]*i; dsum[1][i]=dsum[1][i-1]+b[i]*i; } LL ans=1e18; for(int i=1;i<n;i++) { dp[0][i]=querydown(1,0,i);//假设1到i修的都是0,求1到i里喜欢1的人到i+1位置的花费 dp[1][i]=querydown(0,0,i);//假设1到i修的都是1,求1到i里喜欢0的人到i+1位置的花费 for(int j=1;j<i;j++) { dp[0][i]=min(dp[0][i],dp[1][j]+query(1,j,i)); dp[1][i]=min(dp[1][i],dp[0][j]+query(0,j,i)); } ans=min(ans,dp[0][i]+queryup(0,i,n)); ans=min(ans,dp[1][i]+queryup(1,i,n)); } printf("Case #%d: %lld\n",cas,ans); } return 0; }
- HDU 5550 Game Rooms (dp+前缀和预处理)
- HDU 5550 Game Rooms(dp)
- HDU 5550 Game Rooms(DP)
- HDU 5550 Game Rooms (dp)
- hdu 5550 Game Rooms
- hdu 5084 前缀和预处理
- HDU 5550 dp + 前缀和优化
- HDU 2089 不要62 预处理+前缀和
- 【DP+预处理|最长连续子序列】HDU-1505 City Game
- 预处理之前缀和
- HDU 4436 str2int 后缀数组(前缀和预处理)
- hdu 5317 (普通素数筛的应用)+(前缀和预处理)
- hdu 4681 String (dp预处理+枚举)
- hdu 4455 Substrings (DP 预处理思路)
- hdu 3944 DP? (预处理+卢卡斯定理)
- LightOJ 1007 - Mathematically Hard (欧拉筛+预处理前缀和)
- Codeforces 106 D. Treasure Island(前缀和预处理)
- NOIP复赛复习(十六)预处理与前缀和
- sublime text 3 中文位置偏下解决方法
- 895D
- Create a .desktop file that opens and execute a command in a terminal
- C和指针之高级指针话题通过函数指针实现在链表中找到特定的值
- mysql近500w条数据建立索引后查询速度还是提升很多
- HDU 5550 Game Rooms (dp+前缀和预处理)
- JingS-5
- Sql语句 读取Excel
- 临时内核页表的建立过程
- 单点登录原理与简单实现
- struts2+velocity搭建
- keras搬砖系列-分类
- 搜狐2017秋招研发工程师笔试 —— 袋鼠过河(贪心、动态规划、转为图)
- 【3分钟带你学】接口