結果
| 問題 |
No.1017 Reiwa Sequence
|
| ユーザー |
|
| 提出日時 | 2020-04-03 22:26:45 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,387 bytes |
| コンパイル時間 | 2,462 ms |
| コンパイル使用メモリ | 214,340 KB |
| 最終ジャッジ日時 | 2025-01-09 13:14:31 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 28 TLE * 22 |
ソースコード
#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
unordered_map<int64_t, int> cur, prv;
vector<int> order(n);
iota(ALL(order), 0);
sort(ALL(order), [&](int i, int j) { return a[i] < a[j]; });
REP (i, n) {
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;
}
}
if (not cur.count(0)) {
return vector<int>();
}
// for (auto it : cur) {
// cerr << it.first << " (" << it.second << ")" << endl;
// }
// reconstruct
vector<int> x(n);
for (int64_t value = 0; ; ) {
int i = cur[value];
if (value - a[i] == 0) {
x[i] = +1;
break;
} else if (cur.count(value - a[i]) and cur[value - a[i]] < i) {
x[i] = +1;
value -= a[i];
} else if (cur.count(value + a[i]) and cur[value + a[i]] < i) {
x[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;
}