結果

問題 No.1017 Reiwa Sequence
ユーザー koyoprokoyopro
提出日時 2020-06-12 01:05:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 2,465 bytes
コンパイル時間 2,468 ms
コンパイル使用メモリ 146,676 KB
実行使用メモリ 28,512 KB
最終ジャッジ日時 2023-09-16 10:08:14
合計ジャッジ時間 16,393 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
26,952 KB
testcase_01 AC 18 ms
26,676 KB
testcase_02 AC 13 ms
26,968 KB
testcase_03 AC 32 ms
27,008 KB
testcase_04 AC 14 ms
26,892 KB
testcase_05 AC 18 ms
27,204 KB
testcase_06 AC 16 ms
27,128 KB
testcase_07 AC 14 ms
26,948 KB
testcase_08 AC 13 ms
26,956 KB
testcase_09 AC 39 ms
26,880 KB
testcase_10 AC 52 ms
26,876 KB
testcase_11 AC 38 ms
26,956 KB
testcase_12 AC 50 ms
26,936 KB
testcase_13 AC 35 ms
26,872 KB
testcase_14 AC 40 ms
26,936 KB
testcase_15 AC 45 ms
26,928 KB
testcase_16 AC 40 ms
27,032 KB
testcase_17 AC 74 ms
26,792 KB
testcase_18 AC 41 ms
27,148 KB
testcase_19 AC 128 ms
26,932 KB
testcase_20 AC 144 ms
27,136 KB
testcase_21 AC 150 ms
26,948 KB
testcase_22 AC 144 ms
27,196 KB
testcase_23 AC 134 ms
26,716 KB
testcase_24 AC 97 ms
26,700 KB
testcase_25 AC 118 ms
27,072 KB
testcase_26 AC 91 ms
26,872 KB
testcase_27 AC 113 ms
26,844 KB
testcase_28 AC 109 ms
26,992 KB
testcase_29 AC 59 ms
26,936 KB
testcase_30 AC 77 ms
26,976 KB
testcase_31 AC 99 ms
27,444 KB
testcase_32 AC 102 ms
26,808 KB
testcase_33 AC 48 ms
26,940 KB
testcase_34 AC 74 ms
26,908 KB
testcase_35 AC 13 ms
26,676 KB
testcase_36 AC 29 ms
27,124 KB
testcase_37 AC 75 ms
27,136 KB
testcase_38 AC 17 ms
26,772 KB
testcase_39 AC 103 ms
26,892 KB
testcase_40 AC 132 ms
27,972 KB
testcase_41 AC 164 ms
27,872 KB
testcase_42 AC 176 ms
27,896 KB
testcase_43 AC 143 ms
27,864 KB
testcase_44 AC 34 ms
27,896 KB
testcase_45 AC 152 ms
27,964 KB
testcase_46 AC 138 ms
27,848 KB
testcase_47 AC 113 ms
27,984 KB
testcase_48 AC 67 ms
28,512 KB
testcase_49 AC 39 ms
28,464 KB
testcase_50 AC 38 ms
28,420 KB
testcase_51 AC 11 ms
26,972 KB
testcase_52 AC 77 ms
27,008 KB
testcase_53 AC 73 ms
26,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)
#if DEBUG
#define debug(...) //printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 153450
#define MAX 1500000
#else
#define SIZE 153450
#define MAX 1500000
#endif

int N,A[SIZE],B[SIZE];
vector<int> DP(SIZE*20, -1);
auto dp = DP.begin();

void update(int i, int j) {
    int k = dp[j];
    debug("A[%lld]=%lld, j=%lld, k=%lld\n",i,A[i],j,k);
    if (A[k]==j) {
        B[k]=-A[k];
    }
    else if (A[k]==-j) {
        B[k]=A[k];
    }
    else if (j-A[k] >= -MAX && dp[j-A[k]] >= 0 && dp[j-A[k]] < k) {
        B[k]=-A[k];
        update(k, j-A[k]);
    }
    else if (j+A[k] <= MAX && dp[j+A[k]] >= 0 && dp[j+A[k]] < k) {
        B[k]=A[k];
        update(k, j+A[k]);
    }
}

bool check() {
    REP(i,N) {
        FOR(j,-MAX,MAX+1)if(dp[j]>=0 && dp[j]!=i) {
            if (abs(j)==A[i]) {
                // 経路復元
                if (A[i]==j) {
                    B[i]=A[i];
                }
                else if (A[i]==-j) {
                    B[i]=-A[i];
                }
                update(i,j);
                return true;
            }
            int n0 = j-A[i];
            if (-MAX <= n0 && dp[n0]<0) dp[n0] = i;
            int n1 = j+A[i];
            if (MAX >= n1 && dp[n1]<0) dp[n1] = i;
        }
        if (dp[A[i]]<0) dp[A[i]]=i;
        if (dp[-A[i]]<0) dp[-A[i]]=i;
    }
    return false;
}

void solve() {
    cin>>N;
    REP(i,N)cin>>A[i];
    dp += SIZE*10;
    int ret = check();
    if (!ret) {
        cout << "No" << endl;
        return;
    }
    cout << "Yes" << endl;
    REP(i,N){
        cout << B[i] << " ";
    }
    cout << endl;
}

0