【dp】hdu 6024 Building Shops

                                          Building Shops

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3723    Accepted Submission(s): 1239


 

Problem Description

HDU’s n 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 n classrooms.

The total cost consists of two parts. Building a candy shop at classroom i would have some cost ci. For every classroom P without any candy shop, then the distance between P and the rightmost classroom with a candy shop on P'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), denoting the number of the classrooms.
In the following n lines, each line contains two integers xi,ci(−109≤xi,ci≤109), denoting the coordinate of the i-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.

题意:有n个教室,第i个教室建糖果店的花费为cost[i],第i个教室不建糖果店的花费为它到它左边离它最近的糖果店的距离。如果一个教室没有糖果店,它左边一定存在教室有糖果店。

思路:错误版本最后再上。首先第一个教室一定要选,之后每个糖果店都有建和不建两种选择。dp[i][0]代表第i个教室不选,1代表选

dp[i][1]=min(dp[i-1][0],dp[i-1],1)+s[i].cost

dp[i][0]=min(dp[i][0],dp[j][1]+dist[j][i]);

distp[j][i]代表上一个建的教室是j,(j+1到i这些教室都每建),j+1~i的总共i-j个教室的总成本

dist[j][i]=dist[j][i-1]+s[i].pos-s[j].pos   关于dist的处理有O(n)复杂度的方法,即j+1到j的距离被算了i-j次,依次加上相邻两个教室的距离*它们被算的次数。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;


typedef long long ll;
const int maxn = 3010;
const ll INF = 1e18;
ll dp[maxn][2];
ll dist[maxn][maxn];    //dist[i][j]这个是预处理出上一个是i,j位置不建的最小花费
//ll last[maxn][2];
struct node
{
    ll pos;
    ll cost;
}s[maxn];
int n;
bool cmp(node a,node b)
{
    return a.pos<b.pos;
}
void pre()
{
    for(int i=1;i<=n;++i)
    {
        dist[i][i]=0;
        for(int j=i+1;j<=n;++j)
            dist[i][j]=dist[i][j-1]+s[j].pos-s[i].pos;  //上一个有糖的位置在i,从i+1到j都是无糖的,计算这些教室的距离花费
    }
}
int main()
{

    while(cin>>n)
    {
        for(int i=1;i<=n;++i)
            cin>>s[i].pos>>s[i].cost;
        sort(s+1,s+1+n,cmp);
        pre();
        for(int i=1;i<=n;++i)
            dp[i][0]=dp[i][1]=INF;
        dp[1][1]=s[1].cost;
        for(int i=2;i<=n;++i)
        {
            dp[i][1]=min(dp[i-1][0],dp[i-1][1])+s[i].cost;
            for(int j=i-1;j>=1;--j)
            {
                dp[i][0]=min(dp[i][0],dp[j][1]+dist[j][i]);
            }
        }

        cout<<min(dp[n][0],dp[n][1])<<endl;

    }
    return 0;
}

网上还流传另一种思路,和这个异曲同工。

dp[i][j]代表第i个教室距离它最近的糖果店是j  

最终答案是dp[n][1~n]

j代表左侧糖果店_博客

dp[i][j]=dp[i-1][j]+s[i].pos-s[j].pos

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;


typedef long long ll;
const int maxn = 3010;
const ll INF = 1e18;
ll dp[maxn][maxn];//dp[i][j]距离i最近的糖果店是j
ll m[maxn];//m[i]考虑到第i个时的最小值
struct node
{
    ll pos;
    ll cost;
}s[maxn];
int n;
bool cmp(node a,node b)
{
    return a.pos<b.pos;
}

int main()
{

    while(cin>>n)
    {
        for(int i=1;i<=n;++i)
            cin>>s[i].pos>>s[i].cost;
        sort(s+1,s+1+n,cmp);
        for(int i=1;i<=n;++i) m[i]=INF; m[1]=s[1].cost;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
            dp[i][j]=INF;
        dp[1][1]=s[1].cost;
        for(int i=2;i<=n;++i)
        {
            dp[i][i]=m[i-1]+s[i].cost;
            m[i]=dp[i][i];
            for(int j=1;j<i;++j)
            {
                 dp[i][j]=dp[i-1][j]+s[i].pos-s[j].pos;
                 m[i]=min(m[i],dp[i][j]);
            }
        }
        cout<<m[n]<<endl;

    }
    return 0;
}

错误,dp状态定义和之前一样,尚未找到wa的原因。

dp[i][0]=min(dp[i-1][0]+s[i].pos-last[i-1][0],dp[i-1][1]+s[i].pos-s[i-1].pos)

 last[i-1]记录的是上一个糖果店的位置,其中last[i-1][0]是上个店未建糖果店,那么之前最近的糖果店的位置,last[i-1][1]=s[i-1].pos,上个店建了糖果店。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3010;
const ll INF = 1e18;
ll dp[maxn][2];
ll last[maxn][2];           //last[i][0]  last[i][1]存的是上一个糖果店的位置
struct node
{
    ll pos;
    ll cost;
}shop[maxn];
bool cmp(node a,node b)
{
    return a.pos<b.pos;
}
int main()
{
    int n;
    while(cin >> n)
    {
        for(int i = 1; i <= n; i++)
            dp[i][0] = dp[i][1] = INF;

        for(int i = 1; i <= n; i++)
        {
            cin>>shop[i].pos>>shop[i].cost;
        }

        sort(shop+1,shop+1+n,cmp);

        for(int i = 1; i <= n; i++)
        {
            last[i][0] = shop[1].pos;    //我的last[i][0]存的是上一个糖果店的位置
            last[i][1] = shop[i].pos;    //last[i][1]存的是自己的位置

        }
        dp[1][1]=shop[1].cost;

    
  
        for(int i=2;i<=n;++i)
        {
            dp[i][1]=min(dp[i-1][0],dp[i-1][1])+shop[i].cost;
            last[i][1]=shop[i].pos;
            dp[i][0]=min(dp[i-1][0]+shop[i].pos-last[i-1][0],dp[i-1][1]+shop[i].pos-last[i-1][1]);
            //cout<<dp[i][0]<<' '<<dp[i][1]<<endl;
            if(dp[i][0]==dp[i-1][1]+shop[i].pos-last[i-1][1])   //价格相同的时候应该优先建糖果店
                last[i][0]=last[i-1][1];
            else last[i][0]=last[i-1][0];       //事实上就是pos[i];

        }
        ll ans=min(dp[n][0],dp[n][1]);
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值