結果
| 問題 |
No.2741 Balanced Choice
|
| コンテスト | |
| ユーザー |
zatsu308
|
| 提出日時 | 2024-04-20 12:17:49 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 46 ms / 2,000 ms |
| コード長 | 1,796 bytes |
| コンパイル時間 | 1,856 ms |
| コンパイル使用メモリ | 176,068 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-12 07:36:57 |
| 合計ジャッジ時間 | 2,880 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 10 |
コンパイルメッセージ
main.cpp: In lambda function:
main.cpp:58:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
58 | auto [wei, val] = a[i];
| ^
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 998244353;
template<ll mod>
struct Mint {
using M=Mint; ll v;
M& put(ll x) { v=(x<mod)?x:x-mod; return *this; }
Mint(ll x=0) { put(x%mod+mod); }
M operator+(M m) {return M().put(v+m.v);}
M operator-(M m) {return M().put(v+mod-m.v);}
M operator*(M m) {return M().put(v*m.v%mod);}
M operator/(M m) {return M().put(v*m.inv().v%mod);}
// BEGIN IGNORE
M operator+=(M m) { return put(v+m.v); }
M operator-=(M m) { return put(v+mod-m.v); }
M operator*=(M m) { return put(v*m.v%mod); }
M operator/=(M m) { return put(v*m.inv().v%mod); }
// END IGNORE
bool operator==(M m) { return v==m.v; }
M pow(ll m) const {
M x=v, res=1;
while (m) {
if (m&1) res=res*x;
x=x*x; m>>=1;
}
return res;
}
M inv() { return pow(mod-2); }
};
using mint = Mint<mod>;
int main(){
cin.tie(0);
cin.sync_with_stdio(0);
int n, w, d;
cin >> n >> w >> d;
vector<int> t(n), a(n), v(n);
for(int i = 0; i < n; ++i){
cin >> t[i] >> a[i] >> v[i];
}
vector<pair<int,int>> t0, t1;
for(int i = 0; i < n; ++i){
if(t[i] == 0)t0.emplace_back(a[i], v[i]);
if(t[i] == 1)t1.emplace_back(a[i], v[i]);
}
auto f = [&](vector<pair<int,int>> a){
vector<int> dp(w + 1, -1e9);
dp[0] = 0;
for(int i = 0; i < a.size(); ++i){
auto nex = dp;
auto [wei, val] = a[i];
for(int j = 0; j <= w; ++j){
if(j + wei <= w)nex[j + wei] = max(nex[j + wei], dp[j] + val);
}
swap(dp, nex);
}
return dp;
};
auto r1 = f(t0);
auto r2 = f(t1);
int ans = 0;
for(int i = 0; i <= w; ++i){
for(int j = 0; i + j <= w; ++j){
if(abs(i - j) <= d)
ans = max(ans, r1[i] + r2[j]);
}
}
cout << ans << endl;
}
zatsu308