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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cstdlib> 6 #include<cmath> 7 #define lson l, m, rt<<1 8 #define rson m+1, r, rt<<1|1 9 #define IO ios::sync_with_stdio(false);cin.tie(0); 10 #define INF 1e9 11 typedef long long ll; 12 using namespace std; 13 int n, m; 14 ll dp[100010][2]; 15 typedef struct { 16 ll x, c; 17 }Node; 18 Node node[100010]; 19 bool cmp(const Node a, const Node b) 20 { 21 return a.x<b.x; 22 } 23 int main() 24 { 25 while(cin >> n){ 26 for(int i = 1; i <= n; i++){ 27 cin >> node[i].x >> node[i].c; 28 } 29 for(int i = 1; i <= n; i++){ 30 dp[i][0] = INF; 31 dp[i][1] = INF; 32 } 33 dp[0][0]=0;dp[0][1]=0; 34 sort(node+1, node+n+1, cmp); 35 for(int i = 1; i <= n; i++){ 36 dp[i][1] = min(dp[i-1][0], dp[i-1][1])+node[i].c; 37 ll tmp = 0; 38 for(int j = i-1; j >= 1; j--){ 39 tmp += (i-j)*(node[j+1].x - node[j].x);//次数*长度 40 dp[i][0] = min(dp[i][0], dp[j][1]+tmp); 41 } 42 } 43 cout << min(dp[n][1], dp[n][0]) << endl; 44 } 45 return 0; 46 }