結果
| 問題 |
No.2741 Balanced Choice
|
| コンテスト | |
| ユーザー |
0214sh7
|
| 提出日時 | 2024-04-21 08:06:06 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,908 bytes |
| コンパイル時間 | 2,394 ms |
| コンパイル使用メモリ | 204,120 KB |
| 最終ジャッジ日時 | 2025-02-21 07:56:34 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 WA * 2 |
ソースコード
#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 << i << " ";}cout << endl;
// REP(i,W+1){cout << (dp1[i]<=-INF/2?-1:dp1[i]) << " ";}cout << endl;
// REP(i,W+1){cout << (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(i-D,k+1);
// cout << i MM k MM dp1[i]+r2 << endl;
Ans = max(Ans,dp1[i]+r2);
ll r1 = SEG1.query(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;
}
0214sh7