Codeforces Beta Round #38
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
- 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.
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.
OutputOutput the single number — the least fine you will have to pay.
ExamplesinputCopy3
2 3
3 4
1 2
output5
inputCopy4
1 7
3 1
5 10
6 1
output11
題意:
給你n個球,一開始球都往左滾,使球停止有兩種方法。
1.把它釘起來,給你一個花費。
2.讓它滾到釘起來的球旁邊。花費為距離。
讓你求使所有球停止的最少花費。
POINT:
dp[i],代表釘下i這個球后(1-i)球的最少花費。
打表cost[i][j]陣列代表,j+1到i這些球全部滾到j這個球上的距離。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 3333;
#define LL long long
struct node
{
LL x,cost;
friend bool operator < (node a,node b)
{
return a.x<b.x;
}
}a[3333];
LL cost[maxn][maxn];
LL dp[maxn];
int main()
{
LL n;
scanf("%lld",&n);
for(LL i=1;i<=n;i++){
scanf("%lld%lld",&a[i].x,&a[i].cost);
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
cost[j][i]=a[j].x-a[i].x;
}
for(int j=i+1;j<=n;j++){
cost[j][i]+=cost[j-1][i];
}
}
/*for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
printf("%lld ",cost[j][i]);
}
printf("\n");
}*/
dp[1]=a[1].cost;
for(int i=2;i<=n;i++){
dp[i]=13278126378612783;
for(int j=1;j<i;j++){
dp[i]=min(dp[i],dp[j]+cost[i-1][j]+a[i].cost);
}
}
LL Min=13278126378612783;
for(int i=1;i<=n;i++){
Min=min(dp[i]+cost[n][i],Min);
}
printf("%lld\n",Min);
return 0;
}