結果

問題 No.2741 Balanced Choice
ユーザー 0214sh70214sh7
提出日時 2024-04-21 08:22:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 3,959 bytes
コンパイル時間 2,382 ms
コンパイル使用メモリ 204,508 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 08:22:29
合計ジャッジ時間 3,981 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
5,248 KB
testcase_01 AC 76 ms
5,376 KB
testcase_02 AC 78 ms
5,376 KB
testcase_03 AC 79 ms
5,376 KB
testcase_04 AC 76 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 52 ms
5,376 KB
testcase_08 AC 66 ms
5,376 KB
testcase_09 AC 47 ms
5,376 KB
testcase_10 AC 53 ms
5,376 KB
testcase_11 AC 66 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> PP;
// #define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define ORREP(i,n) for(ll i=(n);i>=1;--i)
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false

template<typename T>
class segmenttree{
    // Copyright (c) 2023 0214sh7
    // https://github.com/0214sh7/library/
    private:
    int n;
    
    std::vector<T> dat;
    std::function<T(T,T)> calc;
    T identity;
    public:
    
    void init(int N,std::function<T(T,T)> func,T Identity){
        n=1;
        while(n<N)n*=2;
        dat.resize(2*n-1);
        for(int i=0;i<2*n-1;++i){
            dat[i]=Identity;
        }
        calc = func;
        identity = Identity;
    }
    
    segmenttree(int N,std::function<T(T,T)> func,T Identity){
        init(N,func,Identity);
    }
    
    void update(int k,T a){
        k+=n-1;
        dat[k]=a;
        while(k>0){
            k=(k-1)/2;
            dat[k]=calc(dat[2*k+1],dat[2*k+2]);
        }
    }
    
    T query(int a,int b){
        a+=n-1;
        b+=n-1;
        T L= identity,R = identity;
        while(a < b){
            if(a % 2 == 0){
                L = calc(L,dat[a]);
                a++;
            }
            a = (a-1)/2;
            if(b % 2 == 0){
                R = calc(dat[b-1],R);
                b--;
            }
            b = (b-1)/2;
        }
        R = calc(L,R);
        return R;
    }
    
};

int main(void){
    //cin.tie(nullptr);
    //ios::sync_with_stdio(false);
    
    ll N,W,D;
    cin >> N >> W >> D;
    vector<ll> W1,W2,V1,V2;
    REP(i,N){
        ll t,w,v;
        cin >> t >> w >> v;
        if(t==1){
            W1.push_back(w);
            V1.push_back(v);
        }else{
            W2.push_back(w);
            V2.push_back(v);
        }
    }
    
    function<vector<ll>(vector<ll>,vector<ll>)> knap = [&](vector<ll> w, vector<ll> v){
        // dp[i][j] := iまで見た、重さがj以下の最大価値
        
        vector<ll> dp(W+1,-INF);
        dp[0] = 0;
        REP(i,w.size()){
            vector<ll> tmp(W+1,-INF);
            REP(j,W+1){
                tmp[j] = max(tmp[j],dp[j]);
                if(j+w[i] <= W){
                    tmp[j+w[i]] = max(tmp[j+w[i]],dp[j]+v[i]);
                }
            }
            
            swap(dp,tmp);
        }
        
        return dp;
    };
    
    vector<ll> dp1 = knap(W1,V1), dp2 = knap(W2,V2);
    
    // REP(i,W+1){cout << setw(2) << i << " ";}cout << endl;
    // REP(i,W+1){cout << setw(2) << (dp1[i]<=-INF/2?-1:dp1[i]) << " ";}cout << endl;
    // REP(i,W+1){cout << setw(2) << (dp2[i]<=-INF/2?-1:dp2[i]) << " ";}cout << endl;
    
    std::function<long long(long long,long long)> func = [](long long a,long long b){
        return max(a,b);
    };
    
    segmenttree<long long> SEG1(W+1,func,-INF);
    for(int i=0;i<=W;i++){
        SEG1.update(i,dp1[i]);
    }
    
    segmenttree<long long> SEG2(W+1,func,-INF);
    for(int i=0;i<=W;i++){
        SEG2.update(i,dp2[i]);
    }
    
    ll Ans = 0;
    REP(i,W+1){
        
        ll k = min(W-i,i+D);
        if(k>=i-D){
            
            ll r2 = SEG2.query(max(0ll,i-D),k+1);
            // cout << i MM k MM dp1[i]+r2 << endl;
            Ans = max(Ans,dp1[i]+r2);
            
            ll r1 = SEG1.query(max(0ll,i-D),k+1);
            // cout << k MM i MM r1+dp2[i] << endl << endl;
            Ans = max(Ans,r1+dp2[i]);
            
        }
    }
    
    cout << Ans << endl;
    
    return 0;
    
}
0