2018 AICCSA Programming Contest H. Win Strategy 题解(01背包变形)

题目链接

题目大意

你在[1,L]有时间玩n个游戏,每个游戏你可以选择在a[i]开始的任意时间以后玩连续的b[i]秒,你一次只能玩一个游戏,求你最多玩多少个游戏

题目思路

这个我最开始以为是贪心。后面发现显然不是,实话实说感觉一点都不像dp,除了数据范围有一点像,这个和普通dp不同的就是他这个背包必须要大于a[i]+b[i]-1才能进行背包。然后直接背包一下就行了。

代码

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e3+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-10;
int n,l,a[maxn],b[maxn],dp[maxn];
signed main(){
    int _;scanf("%d",&_);
    while(_--){
        memset(dp,0,sizeof(dp));
        scanf("%d%d",&n,&l);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&a[i],&b[i]);
        }
        for(int i=1;i<=n;i++){
            for(int j=l;j>=a[i]+b[i]-1;j--){
                dp[j]=max(dp[j],dp[j-b[i]]+1);
            }
        }
        printf("%d\n",dp[l]);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_46209312/article/details/107801183