結果
| 問題 |
No.1508 Avoid being hit
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2021-05-14 23:31:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 61 ms / 3,000 ms |
| コード長 | 3,642 bytes |
| コンパイル時間 | 6,483 ms |
| コンパイル使用メモリ | 254,328 KB |
| 最終ジャッジ日時 | 2025-01-21 12:24:30 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 |
ソースコード
#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
VI a(q), b(q);
rep(i, q) cin >> a[i], a[i]--;
rep(i, q) cin >> b[i], b[i]--;
VVI from(n);
int l = 0, r = n - 1;
vector<vector<P>> history(n);
rep(i, q) {
if (a[i] > b[i]) swap(a[i], b[i]);
if (a[i] == b[i]) b[i] = n + 5;
// cout << i << ' ' << a[i] << ' ' << b[i] << endl;
if (l == r) {
if (l == a[i] || l == b[i]) {
cout << "NO" << '\n';
return 0;
}
if (0 <= l - 1) {
l--;
history[l].emplace_back(i, l + 1);
}
if (r + 1 < n) {
r++;
history[r].emplace_back(i, r - 1);
}
} else if (l + 1 == r) {
if (l == a[i] || l == b[i]) l++;
if (r == a[i] || r == b[i]) r--;
if (!(l <= r)) {
cout << "NO" << '\n';
return 0;
}
if (0 <= l - 1) {
l--;
history[l].emplace_back(i, l + 1);
}
if (r + 1 < n) {
r++;
history[r].emplace_back(i, r - 1);
}
} else if (a[i] + 1 == b[i]) {
//cout << "!!" <<i << ' ' << a[i] << ' ' << l << endl;
if (a[i] == l) {
l += 2;
} else if (b[i] == r) {
r -= 2;
} else if (b[i] == l) {
l += 1;
} else if (a[i] == r) {
r -= 1;
} else if (l < a[i] && b[i] < r) {
history[a[i]].emplace_back(i, a[i] - 1);
history[b[i]].emplace_back(i, b[i] + 1);
}
if (0 <= l - 1) {
l--;
history[l].emplace_back(i, l + 1);
}
if (r + 1 < n) {
r++;
history[r].emplace_back(i, r - 1);
}
} else {
if (l == a[i] || l == b[i]) {
l += 1;
}
if (r == a[i] || r == b[i]) {
r -= 1;
}
for(int x: {a[i], b[i]}) {
if (l < x && x < r) {
history[x].emplace_back(i, x - 1);
}
}
if (0 <= l - 1) {
l--;
history[l].emplace_back(i, l + 1);
}
if (r + 1 < n) {
r++;
history[r].emplace_back(i, r - 1);
}
}
// cout << l +1<< ' ' << r+1 << endl;
}
cout << "YES" << '\n';
VI ans(q);
int now = l;
rrep(i, q) {
while(!history[now].empty() && history[now].back().first > i) history[now].pop_back();
if (!history[now].empty() && history[now].back().first == i) {
int nxt = history[now].back().second;
history[now].pop_back();
now = nxt;
}
ans[i] = now;
}
cout << ans[0] + 1 << '\n';
rep(i, q) cout << ans[i] + 1 << '\n';
}
Kude