HDU 5550 Game Rooms (dp+前缀和预处理)

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 int
const 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;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值