【Atcoder E Knapsack 2】超大背包

Atc E
题意 升级版的01背包 W竟然到了1e9
真的是一点头绪都没
问了下zls zls说转化一下 你既然要val最大
那么我把所有dp[i]代表到i这个价值需要最少的体积
然后用可达性的01背包取min
最后在对1 - maxv 如果dp[i]<=V 那么这个肯定是可以取到的 直接更新答案

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<#define dbg3(x1,x2,x3) cout<<#x1<<" = "<#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
ll val[105],v[105],dp[100025];

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,V;ll ans = 0;scanf("%d%d",&n,&V);
    for(int i = 1;i<=n;++i) scanf("%lld%lld",&v[i],&val[i]);
    memset(dp,0x3f,sizeof(dp));
    dp[0] = 0;
    for(int i = 1;i<=n;++i)
    {
        for(int j = 100000;j>=val[i];j--)
            dp[j] = min(dp[j],dp[j-val[i]]+v[i]);
    }
    for(int i = 1;i<=1e5;++i) if(dp[i]<=V) ans = max(ans,1ll*i);
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

你可能感兴趣的:(ACM,DP)