結果

問題 No.1017 Reiwa Sequence
ユーザー kimiyukikimiyuki
提出日時 2020-04-03 22:43:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,667 bytes
コンパイル時間 2,666 ms
コンパイル使用メモリ 219,568 KB
実行使用メモリ 214,748 KB
最終ジャッジ日時 2023-09-16 03:46:38
合計ジャッジ時間 12,610 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 96 ms
15,920 KB
testcase_10 AC 619 ms
64,160 KB
testcase_11 AC 22 ms
7,364 KB
testcase_12 AC 292 ms
34,084 KB
testcase_13 AC 669 ms
68,568 KB
testcase_14 AC 18 ms
7,024 KB
testcase_15 AC 83 ms
15,708 KB
testcase_16 AC 21 ms
7,352 KB
testcase_17 AC 648 ms
67,408 KB
testcase_18 AC 20 ms
7,348 KB
testcase_19 TLE -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

vector<int> solve(int n, const vector<int64_t> & a) {
    // find duplicated numbers
    unordered_map<int64_t, int> found;
    REP (i, n) {
        if (found.count(a[i])) {
            int j = found[a[i]];
            vector<int> x(n);
            x[i] = +1;
            x[j] = -1;
            return x;
        }
        found[a[i]] = i;
    }

    // dp
    vector<int> order(n);
    iota(ALL(order), 0);
    sort(ALL(order), [&](int i, int j) { return a[i] > a[j]; });
    // shuffle(ALL(order), default_random_engine());
    auto func = [&](int l, int r) {
        unordered_map<int64_t, int> cur, prv;
        REP3 (i, l, r) {
            int64_t a_i = a[order[i]];
            prv = cur;
            cur.try_emplace(a_i, i);
            for (auto it : prv) {
                cur.try_emplace(it.first + a_i, i);
                cur.try_emplace(it.first - a_i, i);
            }
            if (cur.count(0)) {
                break;
            }
        }
// for (auto it : cur) {
// cerr << it.first << " (" << it.second << ")" << endl;
// }
        return cur;
    };
    auto cur = func(0, n);
    if (not cur.count(0)) {
        return vector<int>();
    }

    // reconstruct
    vector<int> x(n);
    for (int64_t value = 0; ; ) {
        int i = cur[value];
        int64_t a_i = a[order[i]];
        if (value - a_i == 0) {
            x[order[i]] = +1;
            break;
        } else if (cur.count(value - a_i) and cur[value - a_i] < i) {
            x[order[i]] = +1;
            value -= a_i;
        } else if (cur.count(value + a_i) and cur[value + a_i] < i) {
            x[order[i]] = -1;
            value += a_i;
        } else {
            assert (false);
        }
    }
    return x;
}

// generated by online-judge-template-generator (https://github.com/kmyk/online-judge-template-generator)
const string YES = "Yes";
const string NO = "No";
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    constexpr char endl = '\n';
    int N;
    cin >> N;
    vector<int64_t> A(N);
    REP (i, N) {
        cin >> A[i];
    }
    auto x = solve(N, A);
    if (x.empty()) {
        cout << NO << endl;
    } else {
        cout << YES << endl;
        REP (i, N) {
            cout << (A[i] * x[i]) << (i + 1 < N ? ' ' : '\n');
        }
    }
    return 0;
}
0