hdu6024 Building Shops(区间dp)

时间:2022-12-25 02:03:53

https://cn.vjudge.net/problem/HDU-6024

分开考虑某一点种与不种,最后取二者的最小值。

dp[i][1] = min(dp[i-1][0], dp[i-1][1])+c[i];

dp[i][0]则是j从i-1~1递减,判断当j种是,i的最小值,然后取总的最小。

注意dp初始化为INF,以及要开long long

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 1e9
typedef long long ll;
using namespace std;
int n, m;
ll dp[][];
typedef struct {
ll x, c;
}Node;
Node node[];
bool cmp(const Node a, const Node b)
{
return a.x<b.x;
}
int main()
{
while(cin >> n){
for(int i = ; i <= n; i++){
cin >> node[i].x >> node[i].c;
}
for(int i = ; i <= n; i++){
dp[i][] = INF;
dp[i][] = INF;
}
dp[][]=;dp[][]=;
sort(node+, node+n+, cmp);
for(int i = ; i <= n; i++){
dp[i][] = min(dp[i-][], dp[i-][])+node[i].c;
ll tmp = ;
for(int j = i-; j >= ; j--){
tmp += (i-j)*(node[j+].x - node[j].x);//次数*长度
dp[i][] = min(dp[i][], dp[j][]+tmp);
}
}
cout << min(dp[n][], dp[n][]) << endl;
}
return ;
}