結果

問題 No.1017 Reiwa Sequence
ユーザー ferin
提出日時 2020-04-03 23:09:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,046 bytes
コンパイル時間 2,557 ms
コンパイル使用メモリ 196,088 KB
最終ジャッジ日時 2025-01-09 13:40:07
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 45 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

PII pre[21][300001][2];
bool dp[21][300001][2];
int main(void) {
    ll n;
    cin >> n;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];

    bool ok = false;
    vector<ll> ans(n);

    dp[0][150000][0] = true;
    REP(i, min(n, 20LL)) REP(j, 300001) REP(k, 2) {
        if(!dp[i][j][k]) continue;
        // dump(i);
        if(j+a[i] <= 300000) {
            // dump(j+a[i], 1);
            dp[i+1][j+a[i]][1] = true;
            pre[i+1][j+a[i]][1] = PII(a[i], k);
            if(!ok && j+a[i] == 150000) {
                ok = true;
                ll ni = i+1, nj = j+a[i], nk = 1;
                while(ni > 0) {
                    dump(ni, nj, nk, pre[ni][nj][nk]);
                    ans[ni-1] = pre[ni][nj][nk].first;
                    // if(pre[ni][nj][nk].first > 0) {
                    //     ans[ni-1] = a[ni-1];
                    // } else if(pre[ni][nj][nk].first < 0) {
                    //     ans[ni-1] = -a[ni-1];
                    // } else {
                    //     ans[ni-1] = 0;
                    // }
                    ll nni = ni-1;
                    ll nnj = nj - pre[ni][nj][nk].first;
                    ll nnk = pre[ni][nj][nk].second;
                    ni = nni, nj = nnj, nk = nnk;
                }
            }
        }

        if(j-a[i] >= 0) {
            // dump(j-a[i], 1);
            dp[i+1][j-a[i]][1] = true;
            pre[i+1][j-a[i]][1] = PII(-a[i], k);
            if(!ok && j-a[i] == 150000) {
                ok = true;
                ll ni = i+1, nj = j-a[i], nk = 1;
                while(ni > 0) {
                    dump(ni, nj, nk, pre[ni][nj][nk]);
                    ans[ni-1] = pre[ni][nj][nk].first;
                    // if(pre[ni][nj][nk].first > 0) {
                    //     ans[ni-1] = a[ni-1];
                    // } else if(pre[ni][nj][nk].first < 0) {
                    //     ans[ni-1] = -a[ni-1];
                    // } else {
                    //     ans[ni-1] = 0;
                    // }
                    ll nni = ni-1;
                    ll nnj = nj - pre[ni][nj][nk].first;
                    ll nnk = pre[ni][nj][nk].second;
                    ni = nni, nj = nnj, nk = nnk;
                }
            }
        }

        // dump(j, k);
        dp[i+1][j][k] = true;
        pre[i+1][j][k] = PII(0, k);
    }

    if(ok) {
        cout << "Yes\n";
        REP(i, n) cout << ans[i] << (i==n-1 ? '\n' : ' ');
    } else {
        cout << "No\n";
    }

    return 0;
}
0