結果

問題 No.2741 Balanced Choice
ユーザー zatsu308zatsu308
提出日時 2024-04-20 12:17:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,796 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 172,772 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 12:17:52
合計ジャッジ時間 3,072 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
5,248 KB
testcase_01 AC 43 ms
5,376 KB
testcase_02 AC 44 ms
5,376 KB
testcase_03 AC 45 ms
5,376 KB
testcase_04 AC 44 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 24 ms
5,376 KB
testcase_08 AC 34 ms
5,376 KB
testcase_09 AC 23 ms
5,376 KB
testcase_10 AC 26 ms
5,376 KB
testcase_11 AC 34 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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];
      |            ^

ソースコード

diff #

#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;
}
0