【HDU 2159】 FATE 完全背包

最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?
Input
输入数据有多组,对于每组数据第一行输入n,m,k,s(0 < n,m,k,s < 100)四个正整数。分别表示还需的经验值,保留的忍耐度,怪的种数和最多的杀怪数。接下来输入k行数据。每行数据输入两个正整数a,b(0 < a,b < 20);分别表示杀掉一只这种怪xhd会得到的经验值和会减掉的忍耐度。(每种怪都有无数个)
Output
输出升完这级还能保留的最大忍耐度,如果无法升完这级输出-1。
Sample Input
10 10 1 10
1 1
10 10 1 9
1 1
9 10 2 10
1 1
2 2
Sample Output
0
-1
1

题意:m的容量,k个物品,问价值和超过n的前提下能剩下来的最大容量

思路:

完全背包问题。
用dp[i]表示忍耐度为i时能拿到的最大经验
用Left[i]表示忍耐度为i时取到最优解时候剩下来的操作次数。
那么就直接外层i扫一遍1->k种小怪,内层遍历j从1->m,对于当前dp[j],判断取了当前第i个小怪后能否更优,同时还要看转移过来的那歌状态还剩多少次(是否大于1)。

if(dp[j] < dp[j - a[i].w] + a[i].val && Left[j - a[i].w] >= 1) 
	dp[j] = dp[j - a[i].w] + a[i].val, Left[j] =  Left[j - a[i].w] - 1;

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll dp[maxn];    //dp[i] 表示 忍耐度为i时能拿到的最大经验
ll Left[maxn]; // 该数组用来随着dp一起转移,记录剩下的次数

typedef struct Pos
{
    ll val;
    ll w;
}P;
P a[maxn];

int main()
{
    ll n, m, k, s;
    while(cin>>n>>m>>k>>s)
    {
        rep(i,1,m+1) dp[i] = -inf;
        dp[0] = 0;
        rep(i,1,k) a[i].val = read(), a[i].w = read();
        rep(i,0, m+1) Left[i] = s;
        ll ma = 0;
        rep(i,1,k)
        {
            rep(j,a[i].w,m)
            {
                if(dp[j] < dp[j - a[i].w] + a[i].val && Left[j - a[i].w] >= 1) dp[j] = dp[j - a[i].w] + a[i].val, Left[j] =  Left[j - a[i].w] - 1;
                ma = max(ma, dp[j]);
            }
        }
        if(n>ma) cout<<-1<<endl;
        else
        {
            rep(i,0,m) if(dp[i] >= n)
            {
                cout<< m - i << endl;
                break;
            }
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/108068220
今日推荐