結果

問題 No.1017 Reiwa Sequence
ユーザー kimiyukikimiyuki
提出日時 2020-04-03 23:01:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,652 bytes
コンパイル時間 2,970 ms
コンパイル使用メモリ 233,432 KB
実行使用メモリ 272,476 KB
最終ジャッジ日時 2023-09-16 04:16:19
合計ジャッジ時間 11,672 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,504 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 80 ms
15,984 KB
testcase_10 AC 73 ms
15,608 KB
testcase_11 AC 8 ms
4,780 KB
testcase_12 AC 568 ms
62,420 KB
testcase_13 AC 201 ms
31,628 KB
testcase_14 AC 17 ms
7,028 KB
testcase_15 AC 79 ms
15,928 KB
testcase_16 AC 19 ms
7,508 KB
testcase_17 AC 639 ms
69,680 KB
testcase_18 AC 19 ms
7,412 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;
        }
    }

    { // find duplicated difference
        vector<int> order(n);
        iota(ALL(order), 0);
        sort(ALL(order), [&](int i, int j) { return a[i] < a[j]; });
        unordered_multimap<int64_t, pair<int, int> > found;
        REP (i, n) {
            REP3 (j, i + 1, min(n, i + 1000)) {
                int64_t delta = a[order[j]] - a[order[i]];
                for (auto [first, last] = found.equal_range(delta); first != last; ++ first) {
                    auto [k, l] = first->second;
                    if (k != i and k != j and l != i and l != j) {
                        vector<int> x(n);
                        x[order[i]] = +1;
                        x[order[j]] = -1;
                        x[order[k]] = -1;
                        x[order[l]] = +1;
                        return x;
                    }
                }
                found.emplace(delta, make_pair(i, j));
            }
        }
    }

    // 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