Codeforces Problem - 38E - Let's Go Rolling!(DP)

E. Let's Go Rolling!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 xici ( - 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
input
Copy
3
2 3
3 4
1 2
output
5
input
Copy
4
1 7
3 1
5 10
6 1
output
11

题意:

有n个小球,所有小球都在一条线上,以该线为x轴,每个球在x轴上都有各自的位置,所有小球都有各自固定的费用。

初始状态每个小球都未固定,为固定的小球会向x轴的负半轴滚动,直到碰到一个固定的小球。

小球滚动的距离也算是费用。选择哪些小球固定或不固定。

求滚动费用与固定费用之和最小为多少。


先按坐标来排个序

在对小球滚动距离花费进行预处理一下

dp[i] 表示前i个小球的最小花费,且第i个小球固定

最后再算一遍以哪个小球为最后固定的小球花费最少

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 3e3 + 10;
const LL INF = 1e9 * N;
LL n,dp[N],cost[N][N];
struct node{LL x,cost;}a[N];
bool cmp(node a,node b){return a.x < b.x;}
int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].cost);
    sort(a+1, a+1+n,cmp);
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            cost[i][j] = cost[i][j-1] + a[j].x - a[i].x;
        }
    }
    dp[1] = a[1].cost;
    for(int i=2;i<=n;i++){
        dp[i] = INF;
        for(int j=i-1;j>=1;j--){
            dp[i] = min(dp[i], dp[j] + cost[j][i-1] + a[i].cost);
        }
    }
    LL ans = INF;
    for(int i=1;i<=n;i++){
        ans = min(ans, dp[i] + cost[i][n]);
    }
    printf("%lld\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值