zoukankan      html  css  js  c++  java
  • 【luogu 1156】 垃圾陷阱 [动态规划 背包]

    P1156 垃圾陷阱

    蒟蒻哭辽QAQ  想了半个小时 明明嗅到了背包的气息 but emmmmm 状态想偏了QAQ

     设dp[i][j]表示前i个垃圾(注意一定要先按垃圾出现时间排序好),到达高度j时所拥有的最长的生命时间。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define ll long long
     4 #define rg register
     5 const int D=100+5,F=30,T=1000+5,inf=0x3f3f3f3f;
     6 int d,n,f[D][D];
     7 template <class t>void rd(t &x)
     8 {
     9     x=0;int w=0;char ch=0;
    10     while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    11     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    12     x=w?-x:x;
    13 }
    14 
    15 struct litter{int t,h,l;}a[D];
    16 bool cmp(const litter &x,const litter &y) {return x.t<y.t;}
    17 
    18 int main()
    19 {
    20     //freopen("nocows.in","r",stdin);
    21     //freopen("nocows.out","w",stdout);
    22     rd(d),rd(n);
    23     for(rg int i=1;i<=n;++i) rd(a[i].t),rd(a[i].l),rd(a[i].h);
    24     sort(a+1,a+1+n,cmp);
    25     f[0][0]=10;
    26     for(rg int i=1;i<=n;++i)
    27     for(rg int j=0;j<=d;++j)
    28     {
    29         if(f[i-1][j]>=a[i].t) f[i][j]=max(f[i][j],f[i-1][j]+a[i].l);
    30         if(j>=a[i].h&&f[i-1][j-a[i].h]>=a[i].t) f[i][j]=max(f[i][j],f[i-1][j-a[i].h]);
    31      }
    32      int ans=0,res=0;
    33      for(rg int i=1;i<=n;++i)
    34      for(rg int j=0;j<=d;++j)
    35      {
    36          if(f[i][j]>=a[i].t) ans=max(ans,j);
    37          res=max(res,f[i][j]);
    38          if(ans>=d) {printf("%d",a[i].t);exit(0);}
    39      }
    40      printf("%d",res);
    41     return 0;
    42 }
  • 相关阅读:
    WSGI学习系列WSME
    Murano Weekly Meeting 2015.08.11
    Trace Logging Level
    OpenStack Weekly Rank 2015.08.10
    markdown语法测试集合
    css-定位
    html图像、绝对路径和相对路径,链接
    html块、含样式的标签
    html标题、段落、换行与字符实体
    html概述和基本结构
  • 原文地址:https://www.cnblogs.com/lxyyyy/p/10835087.html
Copyright © 2011-2022 走看看