Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 131072/131072 K(Java/Others) Total Submission(s): 0 Accepted Submission(s): 0
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 candyshops in thesen classrooms. The total cost consists of two parts. Building a candy shop at classroom i would have some costci . For every classroomP without any candy shop, then the distance betweenP and the rightmost classroom with a candy shop onP 's left side would be included in the cost too. Obviously, if there isa classroom without any candy shop, there must be a candy shop on its leftside. 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 severaltest 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 integersxi,ci(−109≤xi,ci≤109) , denoting the coordinate of thei -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 asingle 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
#include <bits/stdc++.h> using namespace std; #define mst(a,b) memset((a),(b),sizeof(a)) #define f(i,a,b) for(int i=(a);i<=(b);++i) #define ll long long const int maxn = 3005; const int mod = 1e9+7; const int INF = 0x3f3f3f3f; const double eps = 1e-6; #define rush() int T;scanf("%d",&T);while(T--) struct node { ll x,y; }a[maxn]; ll dp[maxn]; bool cmp(const node &a,const node &b) //比node a 更好的写法 { return a.x<b.x; } int main() { int n; while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) { scanf("%I64d%I64d",&a[i].x,&a[i].y); } sort(a+1,a+n+1,cmp); //使点按照坐标从左往右排序 dp[0]=0; for(int i=1; i<=n; i++) { dp[i]=INF; ll tot=0; for(int j=i; j>0; j--) { tot+=a[j].x; ll cost =a[j].y; //在j处建造商店 ll dis=tot-(i-j+1)*a[j].x; //j右侧所有点与j的距离和 dp[i]=min(dp[i],dp[j-1]+dis+cost); } } printf("%I64d\n",dp[n]); } return 0; }
