poj3616 Milking Time(状态转移方程,类似LIS)

https://vjudge.net/problem/POJ-3616

猛刷简单dp的第一天第二题。

这道题乍一看跟背包很像,不同的在于它是一个区间,背包是定点,试了很久想往背包上套,都没成功。

这题的思路感觉有点陌生,又有点类似于求最长不降子序列的题。

dp[i]为到第i个区间为止(该区间肯定有i)的最大挤奶量,最后从m个里面取最大。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<stack>
 8 #define lson l, m, rt<<1
 9 #define rson m+1, r, rt<<1|1
10 #define INF 0x3f3f3f3f
11 typedef unsigned long long ll;
12 using namespace std;
13 struct Node{
14     int from, to, w;
15 }node[100010];
16 int dp[100010];
17 bool cmp(const Node a, const Node b)
18 {
19     return a.to < b.to;
20 }
21 int main()
22 {
23     int n, m, r;
24     cin >> n >> m >> r;
25     for(int i = 0; i < m; i++){
26         cin >> node[i].from >> node[i].to >> node[i].w;
27     }
28     memset(dp, 0, sizeof(dp));
29     sort(node, node+m, cmp);//按结束时间升序 
30     for(int i = 0; i < m; i++){
31         dp[i] = node[i].w;
32         for(int j = 0; j < i; j++){
33             if(node[i].from >= node[j].to+r){
34                 dp[i] = max(dp[i], dp[j]+node[i].w);//人人为我 
35             }
36         } 
37     }
38     int maxm = -INF;
39     for(int i = 0; i < m; i++){
40         maxm = max(maxm, dp[i]);
41     }
42     cout << maxm << endl;
43     return 0;
44 }

猜你喜欢

转载自www.cnblogs.com/Surprisezang/p/9026899.html