結果

問題 No.2617 容量3のナップザック
ユーザー SSRSSSRS
提出日時 2024-01-26 22:37:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 1,893 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 212,812 KB
実行使用メモリ 49,888 KB
最終ジャッジ日時 2024-01-26 22:37:33
合計ジャッジ時間 7,327 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 132 ms
35,888 KB
testcase_13 AC 139 ms
43,748 KB
testcase_14 AC 128 ms
34,536 KB
testcase_15 AC 93 ms
28,700 KB
testcase_16 AC 112 ms
26,196 KB
testcase_17 AC 116 ms
33,228 KB
testcase_18 AC 114 ms
33,544 KB
testcase_19 AC 22 ms
8,608 KB
testcase_20 AC 129 ms
42,364 KB
testcase_21 AC 64 ms
19,240 KB
testcase_22 AC 147 ms
44,372 KB
testcase_23 AC 149 ms
43,008 KB
testcase_24 AC 9 ms
6,676 KB
testcase_25 AC 103 ms
27,936 KB
testcase_26 AC 122 ms
27,652 KB
testcase_27 AC 52 ms
16,360 KB
testcase_28 AC 7 ms
6,676 KB
testcase_29 AC 59 ms
18,824 KB
testcase_30 AC 135 ms
42,940 KB
testcase_31 AC 119 ms
32,568 KB
testcase_32 AC 153 ms
48,320 KB
testcase_33 AC 168 ms
49,084 KB
testcase_34 AC 170 ms
49,888 KB
testcase_35 AC 162 ms
48,248 KB
testcase_36 AC 219 ms
49,048 KB
testcase_37 AC 170 ms
48,320 KB
testcase_38 AC 157 ms
49,080 KB
testcase_39 AC 184 ms
46,492 KB
testcase_40 AC 165 ms
48,772 KB
testcase_41 AC 81 ms
46,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, K;
  int seed, a, b, m;
  cin >> N >> K >> seed >> a >> b >> m;
  vector<long long> f(N * 2);
  f[0] = seed;
  for (int i = 1; i < N * 2; i++){
    f[i] = (a * f[i - 1] + b) % m;
  }
  vector<int> w(N);
  vector<long long> v(N);
  for (int i = 0; i < N; i++){
    w[i] = f[i] % 3 + 1;
    v[i] = w[i] * f[N + i];
  }
  vector<vector<long long>> V(3);
  for (int i = 0; i < N; i++){
    V[w[i] - 1].push_back(v[i]);
  }
  for (int i = 0; i < 3; i++){
    sort(V[i].begin(), V[i].end(), greater<long long>());
  }
  vector<vector<long long>> S(3);
  for (int i = 0; i < 3; i++){
    int cnt = V[i].size();
    S[i].resize(cnt + 1);
    S[i][0] = 0;
    for (int j = 0; j < cnt; j++){
      S[i][j + 1] = S[i][j] + V[i][j];
    }
  }
  auto get = [&](int i, int j) -> long long {
    return S[i][min(j, (int) S[i].size() - 1)];
  };
  auto solve1 = [&](int a, int b, int c){
    return get(0, a * 3 + b) + get(1, b) + get(2, c);
  };
  auto solve2 = [&](int c){
    int L = 0, R = K - c;
    long long ans = 0;
    while (R - L > 2){
      int ml = (L * 2 + R) / 3;
      int mr = (L + R * 2) / 3;
      long long s1 = solve1(ml, K - c - ml, c);
      long long s2 = solve1(mr, K - c - mr, c);
      ans = max(ans, s1);
      ans = max(ans, s2);
      if (s1 < s2){
        L = ml;
      } else {
        R = mr;
      }
    }
    for (int i = 0; i <= K-c; i++){
      ans = max(ans, solve1(i, K - c - i, c));
    }
    return ans;
  };
  int L = 0, R = K;
  long long ans = 0;
  while (R - L > 2){
    int ml = (L * 2 + R) / 3;
    int mr = (L + R * 2) / 3;
    long long s1 = solve2(ml);
    long long s2 = solve2(mr);
    ans = max(ans, s1);
    ans = max(ans, s2);
    if (s1 < s2){
      L = ml;
    } else {
      R = mr;
    }
  }
  for (int i = L; i <= R; i++){
    ans = max(ans, solve2(i));
  }
  cout << ans << endl;
}
0