Building Shops
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2304 Accepted Submission(s): 797
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.
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.
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.
Sample Input
31 22 33 441 73 15 106 1
Sample Output
511
Source
题意:有n个房子,要在那建造若干糖果店,如果房子P不建造糖果店,那么总花费就要加上一个距离(P最左边的(最右一个有糖果店)的房子之间的距离),一开始以为是贪心,交一发错了,然后想是dp但只把状态想出来了,状态转移方程没想出来。。。
状态:dp[ i ][ j ]表示前 i 个房间最右边的有糖果店的屋子的房子是 j 的最小花费。。。
状态转移方程:
i != j : dp[ i ][ j ] = dp[ i - 1 ][ j ] + d[ i ] - d[ j ] ;
i == j : dp[ i ][ i ] = max(dp[ i - 1 ][ k ]) + c[ i ];( 0<= k < i ) ;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 3e3+7;
ll INF = 1e18;
ll dp[maxn][maxn];
typedef pair<ll , ll> P;
bool cmp(P a, P b)
{
return a.first < b.first;
}
P m[maxn];
int main()
{
int n;
while(cin >> n) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dp[i][j] = INF;
for(int i = 1; i <= n; i++)
{
ll x, c;
scanf("%lld%lld",&x, &c);
m[i].first = x;
m[i].second = c;
}
sort(m+1, m+n+1, cmp);
ll res = INF;
dp[1][1] = m[1].second; //在第一个房间必须建糖果店;
for(int i = 2; i <= n; i++)
{
ll c = INF;
for(int j = 1; j <= i; j++)
{
if(i != j) c = min(c, dp[i - 1][j]), dp[i][j] = dp[i - 1][j] + m[i].first - m[j].first;
else dp[i][j] = c + m[i].second;
if(i == n) res = min(res, dp[i][j]);
}
}
printf("%lld\n", res);
}
return 0;
}