結果

問題 No.1017 Reiwa Sequence
ユーザー simansiman
提出日時 2022-04-12 16:18:34
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,750 bytes
コンパイル時間 7,886 ms
コンパイル使用メモリ 108,684 KB
実行使用メモリ 49,948 KB
最終ジャッジ日時 2023-08-22 21:13:51
合計ジャッジ時間 18,923 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 33 ms
10,252 KB
testcase_20 AC 29 ms
10,464 KB
testcase_21 AC 25 ms
8,416 KB
testcase_22 AC 32 ms
10,584 KB
testcase_23 AC 33 ms
10,688 KB
testcase_24 AC 38 ms
12,120 KB
testcase_25 AC 32 ms
10,824 KB
testcase_26 AC 26 ms
9,364 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 2 ms
4,376 KB
testcase_52 WA -
testcase_53 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

vector<int> A;
map<int, vector<int>> memo1;
map<int, vector<int>> memo2;
int L;

void dfs(int i, int end, int sum, bool ok, vector<int> &nums) {
  if (i == end) {
    if (ok) {
      if (end < L) {
        memo1[sum] = nums;
      } else {
        memo2[sum] = nums;
      }
    }
  } else {
    for (int a = -1; a <= 1; ++a) {
      int v = A[i] * a;
      nums.push_back(v);
      if (a == 0) {
        dfs(i + 1, end, sum + v, ok, nums);
      } else {
        dfs(i + 1, end, sum + v, true, nums);
      }
      nums.pop_back();
    }
  }
}

int main() {
  int N;
  cin >> N;

  if (N == 1) {
    cout << "No" << endl;
    return 0;
  }

  A.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  L = min(N, 22);
  int mid = L / 2;
  vector<int> nums;
  dfs(0, mid, 0, false, nums);
  dfs(mid, L, 0, false, nums);
  int ans[N];
  memset(ans, 0, sizeof(ans));
  bool ok = false;

  for (auto &[sum, nums] : memo1) {
    if (sum == 0) {
      ok = true;
      for (int i = 0; i < nums.size(); ++i) {
        ans[i] = nums[i];
      }
    } else {
      if (memo2.count(-sum)) {
        ok = true;
        vector<int> temp = nums;
        temp.insert(temp.end(), memo2[-sum].begin(), memo2[-sum].end());

        for (int i = 0; i < temp.size(); ++i) {
          ans[i] = temp[i];
        }
      }
    }
  }

  if (ok) {
    for (int i = 0; i < N; ++i) {
      cout << ans[i];
      if (i != N - 1) cout << " ";
    }
    cout << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
0