Codeforces 38E.Let's Go Rolling! (dp)

On a number axis directed from the left rightwards, n marbles with coordinates x1, x2, …, xn are situated. Let’s assume that the sizes of the marbles are infinitely small, that is in this task each of them is assumed to be a material point. You can stick pins in some of them and the cost of sticking in the marble number i is equal to ci, number ci may be negative. After you choose and stick the pins you need, the marbles will start to roll left according to the rule: if a marble has a pin stuck in it, then the marble doesn’t move, otherwise the marble rolls all the way up to the next marble which has a pin stuck in it and stops moving there. If there is no pinned marble on the left to the given unpinned one, it is concluded that the marble rolls to the left to infinity and you will pay an infinitely large fine for it. If no marble rolled infinitely to the left, then the fine will consist of two summands:

the sum of the costs of stuck pins;
the sum of the lengths of the paths of each of the marbles, that is the sum of absolute values of differences between their initial and final positions.
Your task is to choose and pin some marbles in the way that will make the fine for you to pay as little as possible.

Input
The first input line contains an integer n (1 ≤ n ≤ 3000) which is the number of marbles. The next n lines contain the descriptions of the marbles in pairs of integers xi, ci ( - 109 ≤ xi, ci ≤ 109). The numbers are space-separated. Each description is given on a separate line. No two marbles have identical initial positions.

Output
Output the single number — the least fine you will have to pay.

Examples
inputCopy
3
2 3
3 4
1 2
output
5
inputCopy
4
1 7
3 1
5 10
6 1
output
11
题目大意是有一堆滚珠,有各自的在数轴上的位置,对于数轴的每个点可以用一定的钱去固定==滚珠会一直向左边移动,直到碰上某个被固定的点为止,滚珠滚动花费和滚动距离相等的钱,问让所有滚珠全部停止最少需要花费多少钱==
这题一开始就从第n颗滚珠贪心搞了几发,凉凉,于是想要dp一下,没想到dp[i][j]的含义也想错了,一开始以为代表第i颗在第j个位置停下来,然后发现根本写不出转移方程==

其实dp[i][j]代表了前i颗珠子最后一个被固定的点为j,这样表示状态转移方程就可以写了,然而还是一开始写错了==难道是太久没有写dp的缘故么。。。还是太菜
转移方程分两种情况 1.若i==j,则我们需要在前i-1颗中找到在哪个点花费最少,然后加上第i个点的花费。

2.i!=j,那么第i颗在第j个位置停下,由于i不小于j,所以前i-1颗也必定在j位置停下,这样就是dp[i-1][j]+i-j的距离,前缀和处理一波就好。
其实感觉有点像区间dp==

另外还有一个坑点,这题由于答案会特别大,差不多接近long long了,至少是远超int,平时习惯把inf定为0x3f3f3f3f,然后又wa了好几次==
代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define maxn 3010
#define ll long long
#define angel 0x3f3f3f3f
struct stick
{
    ll c;
    ll s;
}num[maxn];
int n;
bool cmp(stick x,stick y)
{
    return x.c<y.c;
}
ll sum[maxn];
ll dp[maxn][maxn];
ll Min(ll x,ll y)
{
    if(x>y)
        return y;
    return x;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld%lld",&num[i].c,&num[i].s);
    sort(num+1,num+1+n,cmp);
    sum[1]=0;
    for(int i=2;i<=n;i++)
        sum[i]=sum[i-1]+(num[i].c-num[i-1].c);//前缀和处理一波距离
    dp[1][1]=num[1].s;//第一颗珠子肯定在第一个点停下来
    for(int i=2;i<=n;i++)
    {
        ll tmp=1e18;//寻找dp[i-1][j]最小值
        for(int j=1;j<=i;j++)
        {
            if(i==j)
                dp[i][j]=tmp+num[j].s;
            else
            {
                tmp=Min(tmp,dp[i-1][j]);
                dp[i][j]=dp[i-1][j]+sum[i]-sum[j];
            }
        }
    }
    ll ans=1e19;//寻找最终结果,不要开小==
    for(int i=1;i<=n;i++)
        ans=Min(ans,dp[n][i]);
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值