luogu P1156 垃圾陷阱

传送门

背包

每种物品有两种方法 也就是不叠高一定吃

所以dp[j]表示高度为j的时候能活多久 初值全是负的

dp[j]<0表示还不能叠到这个高度

所以当j+a[i].h>V 的时候 也就是i之前的东西能保证叠到j而且能活 说明逃出去了

直接输出a[i].time就好

否则活不下去就一个不叠 全吃了 也就是dp[0]

记得物品按时间排序

Code:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define ms(a,b) memset(a,b,sizeof a)
 6 #define rep(i,a,n) for(int i = a;i <= n;i++)
 7 #define per(i,n,a) for(int i = n;i >= a;i--)
 8 #define inf 1000000007
 9 using namespace std;
10 typedef long long ll;
11 typedef double D;
12 #define eps 1e-8
13 ll read() {
14     ll as = 0,fu = 1;
15     char c = getchar();
16     while(c < '0' || c > '9') {
17         if(c == '-') fu = -1;
18         c = getchar();
19     }
20     while(c >= '0' && c <= '9') {
21         as = as * 10 + c - '0';
22         c = getchar();
23     }
24     return as * fu;
25 }
26 //head
27 const int N = 1005;
28 int n,V;
29 
30 struct node {
31     int h,t,x;
32     bool operator < (const node &o) const {
33         return x < o.x;
34     }
35 }a[N];
36 
37 int dp[N];
38 int main() {
39     V = read(),n = read();
40     rep(i,1,n) a[i].x = read(),a[i].t = read(),a[i].h = read();
41     sort(a+1,a+n+1);
42     dp[0] = 10;
43     rep(i,1,n) {
44         per(j,V,0) {
45             if(dp[j] < a[i].x) continue;
46             if(j + a[i].h >= V) {
47                 printf("%d\n",a[i].x);
48                 return 0;
49             }
50             dp[j + a[i].h] = max(dp[j + a[i].h],dp[j]);
51             dp[j] += a[i].t;
52         }
53     }
54     printf("%d\n",dp[0]);
55     return 0;
56 }

 

posted @ 2018-11-08 17:08  白怀潇  阅读(144)  评论(0编辑  收藏  举报