結果

問題 No.332 数列をプレゼントに
ユーザー kyo1kyo1
提出日時 2020-06-21 08:21:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 1,368 bytes
コンパイル時間 2,056 ms
コンパイル使用メモリ 207,000 KB
実行使用メモリ 15,048 KB
最終ジャッジ日時 2023-09-16 18:09:36
合計ジャッジ時間 13,356 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
14,552 KB
testcase_01 AC 14 ms
14,792 KB
testcase_02 AC 8 ms
14,752 KB
testcase_03 AC 8 ms
14,772 KB
testcase_04 AC 5 ms
14,812 KB
testcase_05 AC 158 ms
14,716 KB
testcase_06 AC 185 ms
14,660 KB
testcase_07 AC 250 ms
14,832 KB
testcase_08 AC 201 ms
14,576 KB
testcase_09 AC 27 ms
14,888 KB
testcase_10 AC 53 ms
14,760 KB
testcase_11 AC 148 ms
14,856 KB
testcase_12 AC 256 ms
14,672 KB
testcase_13 AC 196 ms
15,048 KB
testcase_14 AC 118 ms
14,796 KB
testcase_15 AC 220 ms
14,792 KB
testcase_16 AC 197 ms
14,756 KB
testcase_17 AC 228 ms
14,664 KB
testcase_18 AC 238 ms
14,868 KB
testcase_19 AC 204 ms
14,548 KB
testcase_20 AC 224 ms
14,784 KB
testcase_21 AC 240 ms
14,888 KB
testcase_22 AC 219 ms
14,892 KB
testcase_23 AC 249 ms
14,704 KB
testcase_24 AC 251 ms
14,860 KB
testcase_25 AC 250 ms
14,840 KB
testcase_26 AC 256 ms
14,912 KB
testcase_27 AC 240 ms
14,792 KB
testcase_28 AC 243 ms
14,828 KB
testcase_29 AC 245 ms
14,588 KB
testcase_30 AC 250 ms
14,660 KB
testcase_31 AC 253 ms
14,864 KB
testcase_32 AC 249 ms
14,804 KB
testcase_33 AC 237 ms
14,804 KB
testcase_34 AC 227 ms
14,832 KB
testcase_35 AC 260 ms
14,716 KB
testcase_36 AC 5 ms
14,720 KB
testcase_37 AC 244 ms
14,852 KB
testcase_38 AC 245 ms
14,644 KB
testcase_39 AC 248 ms
14,832 KB
testcase_40 AC 264 ms
14,836 KB
testcase_41 AC 260 ms
14,572 KB
testcase_42 AC 261 ms
14,892 KB
testcase_43 AC 268 ms
14,648 KB
testcase_44 AC 271 ms
14,840 KB
testcase_45 AC 272 ms
14,888 KB
testcase_46 AC 119 ms
14,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  int64_t X;
  cin >> N >> X;
  vector<pair<int64_t, int>> A(N);
  for (int i = 0; i < N; i++) {
    int64_t a;
    cin >> a;
    A[i] = make_pair(a, i);
  }
  sort(A.begin(), A.end());
  vector<int> dp(3000000, -1);
  dp[0] = 0;
  int pos = 0;
  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    if (sum + A[i].first > 2000000) {
      pos = i;
      break;
    }
    for (int j = 2000000; j > -1; j--) {
      if ((j == 0 || dp[j] != -1) && dp[j + A[i].first] == -1) {
        dp[j + A[i].first] = i;
      }
    }
    sum += A[i].first;
    pos = i + 1;
  }
  for (int bit = 0; bit < (1 << (N - pos + 1)); bit++) {
    sum = 0;
    for (int i = 0; i < N - pos; i++) {
      if ((bit >> i) & 1) {
        sum += A[i + pos].first;
      }
    }
    sum = X - sum;
    if (sum == 0 || (0 < sum && sum < 3000000 && dp[sum] != -1)) {
      vector<char> res(N, 'x');
      for (int i = 0; i < N - pos; i++) {
        if ((bit >> i) & 1) {
          res[A[i + pos].second] = 'o';
        }
      }
      while (sum != 0) {
        res[A[dp[sum]].second] = 'o';
        sum -= A[dp[sum]].first;
      }
      for (int i = 0; i < N; i++) {
        cout << res[i];
      }
      cout << '\n';
      return 0;
    }
  }
  cout << "No" << '\n';
  return 0;
}
0