zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",dp[0]);
    55     return 0;
    56 }
    > 别忘了 总有人在等着你
  • 相关阅读:
    Ecshop屏幕wap
    SQLite命令
    初识SQLite
    last_insert_id()
    php中的全局变量global(低级错误啊)
    在搜索框加入语音搜索
    解压zip文件出现bash:unzip:commond not found
    DataView.RowFilter使用
    设计自己的模板引擎(一)模板替换中的嵌套循环处理
    没完没了的Cookie,读懂asp.net,asp等web编程中的cookies 
  • 原文地址:https://www.cnblogs.com/yuyanjiaB/p/9930263.html
Copyright © 2011-2022 走看看