結果
問題 |
No.1017 Reiwa Sequence
|
ユーザー |
![]() |
提出日時 | 2020-06-12 01:05:55 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 176 ms / 2,000 ms |
コード長 | 2,465 bytes |
コンパイル時間 | 3,371 ms |
コンパイル使用メモリ | 160,132 KB |
実行使用メモリ | 29,320 KB |
最終ジャッジ日時 | 2024-07-03 11:15:27 |
合計ジャッジ時間 | 34,606 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 50 |
ソースコード
#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; }