hdu6024(dp)

Building Shops
HDU’s nn classrooms are on a line ,which can be considered as a number line. Each classroom has a coordinate. Now Little Q wants to build several candy shops in these nn classrooms.

The total cost consists of two parts. Building a candy shop at classroom ii would have some cost cici. For every classroom PP without any candy shop, then the distance between PP and the rightmost classroom with a candy shop on PP’s left side would be included in the cost too. Obviously, if there is a classroom without any candy shop, there must be a candy shop on its left side.

Now Little Q wants to know how to build the candy shops with the minimal cost. Please write a program to help him.

Input
The input contains several test cases, no more than 10 test cases.

In each test case, the first line contains an integer n(1≤n≤3000)n(1≤n≤3000), denoting the number of the classrooms.

In the following nn lines, each line contains two integers xi,ci(−109≤xi,ci≤109)xi,ci(−109≤xi,ci≤109), denoting the coordinate of the ii-th classroom and the cost of building a candy shop in it.

There are no two classrooms having same coordinate.

Output
For each test case, print a single line containing an integer, denoting the minimal cost.

Sample Input
3
1 2
2 3
3 4
4
1 7
3 1
5 10
6 1

Sample Output
5
11

dp[i][1]表示第i个教室开超市,dp[i][1]=min(dp[i-1][0],dp[i-1][1])+cost[i]
dp[i][0]表示第i个教室不开超市,这时遍历i前的所有教室,计算距离,更新dp[i][0]的值
别忘了先按坐标排个序

#include <bits/stdc++.h>
using namespace std;
const long long inf=1e12;
struct hh
{
   
    long long x,c;
    bool operator < (hh&a)
    {
   
        return this->x<a.x;
    }
    hh():x(0),c(0){
   }
}s[3005];
long long dp[3005][2];
int main()
{
   
    int n;
    while(scanf("%d",&n)!=EOF)
    {
   
        for(int i=0;i<n;i++)
        {
   
            scanf("%lld%lld",&s[i].x,&s[i].c);
        }
        sort(s,s+n);
        memset(dp,0,sizeof(dp));
        dp[0][0]=inf;
        dp[0][1]=s[0].c;
        for(int i=1;i<n;i++)
        {
   
            dp[i][1]=min(dp[i-1][0],dp[i-1][1])+s[i].c;
            dp[i][0]=inf;
            long long t=0;
            for(int j=i-1;j>=0;j--)
            {
   
                t+=(i-j)*(s[j+1].x-s[j].x);
                dp[i][0]=min(dp[i][0],dp[j][1]+t);
            }
        }
        cout<<min(dp[n-1][0],dp[n-1][1])<<'\n';
    }
    return 0;
}

全部评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
11-08 17:36
诺瓦科技_HR
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务