結果

問題 No.1017 Reiwa Sequence
ユーザー knshnbknshnb
提出日時 2020-04-06 18:19:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,885 bytes
コンパイル時間 2,053 ms
コンパイル使用メモリ 204,592 KB
実行使用メモリ 261,580 KB
最終ジャッジ日時 2023-09-16 07:01:11
合計ジャッジ時間 22,536 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
85,036 KB
testcase_01 AC 42 ms
61,640 KB
testcase_02 AC 32 ms
49,724 KB
testcase_03 AC 96 ms
132,020 KB
testcase_04 AC 32 ms
49,908 KB
testcase_05 AC 50 ms
73,456 KB
testcase_06 AC 42 ms
61,624 KB
testcase_07 AC 32 ms
49,780 KB
testcase_08 AC 33 ms
49,868 KB
testcase_09 AC 128 ms
167,224 KB
testcase_10 AC 233 ms
249,264 KB
testcase_11 AC 118 ms
155,508 KB
testcase_12 AC 256 ms
260,716 KB
testcase_13 AC 260 ms
260,736 KB
testcase_14 AC 118 ms
155,348 KB
testcase_15 AC 127 ms
167,380 KB
testcase_16 AC 116 ms
155,336 KB
testcase_17 AC 160 ms
190,548 KB
testcase_18 AC 117 ms
155,428 KB
testcase_19 AC 250 ms
249,188 KB
testcase_20 AC 258 ms
249,176 KB
testcase_21 AC 263 ms
249,244 KB
testcase_22 AC 259 ms
249,292 KB
testcase_23 AC 250 ms
249,252 KB
testcase_24 AC 222 ms
249,196 KB
testcase_25 AC 238 ms
249,176 KB
testcase_26 AC 213 ms
249,136 KB
testcase_27 AC 241 ms
249,140 KB
testcase_28 AC 235 ms
249,064 KB
testcase_29 AC 257 ms
249,192 KB
testcase_30 AC 259 ms
249,232 KB
testcase_31 AC 245 ms
249,236 KB
testcase_32 AC 228 ms
249,192 KB
testcase_33 AC 210 ms
249,128 KB
testcase_34 AC 211 ms
249,188 KB
testcase_35 AC 208 ms
249,264 KB
testcase_36 AC 251 ms
249,268 KB
testcase_37 AC 211 ms
249,272 KB
testcase_38 AC 208 ms
249,152 KB
testcase_39 AC 227 ms
249,264 KB
testcase_40 AC 267 ms
261,248 KB
testcase_41 AC 286 ms
261,248 KB
testcase_42 AC 300 ms
261,248 KB
testcase_43 AC 274 ms
261,328 KB
testcase_44 AC 211 ms
261,308 KB
testcase_45 AC 277 ms
261,216 KB
testcase_46 AC 269 ms
261,200 KB
testcase_47 AC 243 ms
261,244 KB
testcase_48 AC 267 ms
261,332 KB
testcase_49 AC 217 ms
261,580 KB
testcase_50 AC 220 ms
261,536 KB
testcase_51 AC 23 ms
38,212 KB
testcase_52 AC 217 ms
249,208 KB
testcase_53 AC 217 ms
249,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>  // clang-format off
using Int = int;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Mon Apr  6 17:27:48 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
    return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

const Int K = 20;
const Int S = K * 150001;
signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> a(n);
    REP(i, n) std::cin >> a[i];
    Int m = std::min(n, K);
    auto dp = make_vec<short>(m + 1, 2 * S + 1, 0);
    dp[0][S] = 1;
    REP(i, m) {
        auto dp1 = dp[i].begin() + S, dp2 = dp[i + 1].begin() + S;
        REP(j, -S, S) {
            if (dp1[j] == 0) continue;
            dp1[j] = std::min((short)3, dp1[j]);
            dp2[j] += dp1[j];
            dp2[j + a[i]] += dp1[j];
            dp2[j - a[i]] += dp1[j];
        }
    }
    if (dp[m][S] == 1) {
        std::cout << "No" << std::endl;
        return 0;
    }
    Int curj = 0;
    std::vector<Int> b(n);
    for (int i = m - 1; i >= 0; i--) {
        auto dp1 = dp[i].begin() + S, dp2 = dp[i + 1].begin() + S;
        if (dp1[curj - a[i]] > 0) {
            b[i] = a[i];
            curj -= a[i];
        } else if (dp1[curj + a[i]] > 0) {
            b[i] = -a[i];
            curj += a[i];
        } else {
            b[i] = 0;
        }
    }
    assert(curj == 0);
    std::cout << "Yes" << std::endl;
    for (Int x : b) std::cout << x << " ";
    std::cout << std::endl;
}
0