864E(dp + 线段树优化)
来源:互联网 发布:dhc瘦腿丸瘦腿原理知乎 编辑:程序博客网 时间:2024/05/21 10:38
Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.
Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tb seconds after fire started.
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house.
Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.
In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.
33 7 42 6 53 7 6
1122 3
25 6 13 3 5
111
In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11.
In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time.
解题思路:直接dp, 我们先按每个物品的截止日期从小到大排个序,令dp[i][j]表示前i个物品,且第i个物品一定选,当前是第j天能获得的最大值,然后直接从后面所有与状态i, j不冲突的状态转移就行,中间过程记录最优解的前驱就行,可能是我的状态定义的有点不好,这样做的最坏复杂度会达到O(100 * 2000 * 100 * 2000),显然不能接受,我们注意到,我们不用每次从后面所有的状态转移,我们用一棵线段树维护最优解就行。
#include <bits/stdc++.h>using namespace std;int n;int inf = 0x3f3f3f3f;int dp[110][2010];int pre1[110][2010];int pre2[110][2010];struct node{ int id; int t; int d; int p;}Node[110];struct tree{ int l, r; int pr1, pr2; int Max; tree(){ pr1 = pr2 = 0; Max = -inf; }}Tree[2010<<2];struct info{ int i, j; int value;}Info[2010];int num;void pushUp(int i){ int lson = i<<1; int rson = lson|1; if(Tree[lson].Max >= Tree[rson].Max) { Tree[i].Max = Tree[lson].Max; Tree[i].pr1 = Tree[lson].pr1; Tree[i].pr2 = Tree[lson].pr2; } else { Tree[i].Max = Tree[rson].Max; Tree[i].pr1 = Tree[rson].pr1; Tree[i].pr2 = Tree[rson].pr2; }}void build(int i, int l, int r){ Tree[i].l = l; Tree[i].r = r; if(l == r) { if(l == 0) Tree[i].Max = 0; else Tree[i].Max = -inf; return; } int mid = (l + r)>>1; int f = i; i <<= 1; build(i, l, mid); build(i|1, mid + 1, r); pushUp(f);}void update(int i, int loc, int mm, int status1, int status2){ if(Tree[i].l == Tree[i].r) { if(mm > Tree[i].Max) { Tree[i].Max = mm; Tree[i].pr1 = status1; Tree[i].pr2 = status2; } return; } int f = i; i <<= 1; if(loc <= Tree[i].r) update(i, loc, mm, status1, status2); else update(i|1, loc, mm, status1, status2); pushUp(f);}int ss1, ss2;int query(int i, int l, int r){ if(Tree[i].l == l && Tree[i].r == r) { ss1 = Tree[i].pr1; ss2 = Tree[i].pr2; return Tree[i].Max; } i <<= 1; if(r <= Tree[i].r) return query(i, l, r); else if(l >= Tree[i|1].l) return query(i|1, l, r); else { int judge1 = query(i, l, Tree[i].r); int judge2 = query(i|1, Tree[i|1].l, r); if(judge2 >= judge1) return judge2; return query(i, l, Tree[i].r); }}int MM;void init(){ memset(dp, -inf, sizeof(dp)); dp[0][0] = 0; build(1, 0, MM); for(int i = 1; i <= n; i++) { num = 0; for(int j = 0; j <= MM; j++) { pre1[i][j] = 0; pre2[i][j] = 0; if(j >= Node[i].d) continue; if(Node[i].t >= Node[i].d) continue; if(j - Node[i].t < 0) continue; ss1 = ss2 = 0; int M = query(1, 0, j - Node[i].t); dp[i][j] = M + Node[i].p; pre1[i][j] = ss1; pre2[i][j] = ss2; Info[++num].i = i; Info[num].j = j; Info[num].value = dp[i][j]; } for(int j = 1; j <= num; j++) update(1, Info[j].j, Info[j].value, Info[j].i, Info[j].j); }}bool cmp(node n1, node n2){ return n1.d < n2.d;}int ans[110];int res;int main(){ while(~scanf("%d", &n)) { MM = 0; for(int i = 1; i <= n; i++) { scanf("%d%d%d", &Node[i].t, &Node[i].d, &Node[i].p); Node[i].id = i; MM = max(MM, Node[i].d); } sort(Node + 1, Node + n + 1, cmp); init(); int Max = 0; int sta1 = 0; int sta2 = 0; int m = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j <= MM; j++) { if(dp[i][j] > Max) { Max = dp[i][j]; sta1 = i; sta2 = j; } } } if(Max > 0) { res = 0; while(sta1 && sta2) { ans[++res] = Node[sta1].id; int xx = pre1[sta1][sta2]; int yy = pre2[sta1][sta2]; sta1 = xx; sta2 = yy; } printf("%d\n", Max); printf("%d\n", res); int sum = 0; for(int i = res; i >= 1; i--) { if(i == 1) printf("%d\n", ans[i]); else printf("%d ", ans[i]); } } else printf("0\n0\n"); } return 0;}
- 864E(dp + 线段树优化)
- codeforces 271E 离散化+线段树,dp优化
- codeforces E. Trains and Statistic 线段树优化dp
- Codeforces Round #271 (Div. 2) E Pillars(dp+线段树优化)
- Codeforces Round #271 (Div. 2)E. Pillars(dp+线段树优化)
- Codeforces Round #271 (Div. 2) E. Pillars(线段树优化DP)
- codeforces 474E Pillars(线段树+dp)
- Codeforces 474E Pillars dp+线段树
- Codeforces 474E Pillars dp+线段树
- ZJU3349 线段树优化DP
- 数据--dp,线段树优化
- cf343D dp+线段树优化
- 线段树和单调队列优化DP
- zoj 2900 DP(线段树优化)
- 【线段树优化dp】zoj3349 Special Subsequence
- 【DP+线段树优化】[CQBZOJ2933]数据
- hdu 4521(线段树优化dp)
- poj1769 Minimizing maximizer线段树优化dp
- linux下生产环境的redis安装
- unity打开内嵌游戏网页
- Cocos2d C++中调用Java方法
- rpm包安装过程中依赖问题“libc.so.6 is needed by XXX”解决方法
- 关于virtualbox 4.3.12安装centos7重启崩溃的问题。
- 864E(dp + 线段树优化)
- Python 集合(set)及其操作
- 用web制作简历
- Python闭包简谈
- MATLAB图像处理基础知识3 mapminmax
- 浅谈PLC控制系统的可靠性
- "message":"Illegal base64 character a"
- Debug 类
- Spring基础学习笔记