【题解】P1156垃圾陷阱

【题解】P1156 垃圾陷阱

乍看此题,我们感觉状态很多,很复杂。

遇到这类型条件比较多的(dp),我们不要首先考虑全部设出来,而是要看到这些状态的本质。而在这道题目中,时间和高度就是关键。

考虑卡门吃掉垃圾:

  • 时间改变,高度不变。

考虑卡门垫上垃圾:

  • 时间改变,高度改变。

也就是说,垃圾变成了我们的阶段,就不需要存垃圾了。(这话怎么怪怪的)

(dp(x)=y)表示高度为(x)时,还剩下(y)的时间。转移就不写啦咕咕咕

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<bitset>
#include<vector>
#include<map>
#include<ctime>
#include<cstdlib>
#include<set>
#include<bitset>
#include<stack>
#include<list>
#include<cmath>
using namespace std;
#define RP(t,a,b) for(register int (t)=(a),edd_=(b);t<=edd_;++t)
#define DRP(t,a,b) for(register int (t)=(a),edd_=(b);t>=edd_;--t)
#define ERP(t,a) for(int t=head[a];t;t=e[t].to)
#define Max(a,b) ((a)<(b)?(b):(a))
#define Min(a,b) ((a)<(b)?(a):(b))
#define TMP template<class ccf>
typedef long long ll;
TMP inline ccf qr(ccf k){
    char c=getchar();
    ccf x=0;
    int q=1;
    while(c<48||c>57)
    q=c==45?-1:q,c=getchar();
    while(c>=48&&c<=57)
    x=x*10+c-48,c=getchar();
    if(q==-1)
    x=-x;
    return x;
}
const int maxn=1e3+15;
int dp[maxn];// height x,time y can go
// dp[x]=y
int n;
int h;
int t1,t2,t3;
struct qts{
    int a,b,c;
    inline void scan(){
    a=qr(1);
    b=qr(1);
    c=qr(1);
    }
    inline bool operator <(qts x){
    return a<x.a;
    }
}q[maxn];

int main(){
#ifndef ONLINE_JUDGE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
    h=qr(1);
    n=qr(1);
    memset(dp,0xff,sizeof dp);
    dp[0]=10;
    RP(t,1,n)
    q[t].scan();
    sort(q+1,q+n+1);
    
    RP(t,1,n){
    t1=q[t].a;
    t2=q[t].b;
    t3=q[t].c;
    DRP(i,h,0){
        if(dp[i]>=t1){
        if(t3+i>=h)
            return cout<<t1<<endl,0;
        dp[Min(maxn-1,i+t3)]=Max(dp[Min(maxn-1,i+t3)],dp[i]);
        dp[i]+=t2;
        }
    }
    }
    cout<<dp[0]<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/winlere/p/10334072.html