結果
| 問題 |
No.2743 Twisted Lattice
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-18 00:27:59 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,080 ms / 3,000 ms |
| コード長 | 1,857 bytes |
| コンパイル時間 | 2,333 ms |
| コンパイル使用メモリ | 215,896 KB |
| 最終ジャッジ日時 | 2025-02-21 02:54:56 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll h, w, n;
cin >> h >> w >> n;
vector<pair<ll,ll>> a(n);
map<ll, set<ll>> as;
for (int i = 0; i < n; i++) {
cin >> a[i].second >> a[i].first;
as[a[i].first].insert(a[i].second);
}
vector<ll> ans(n, 1e10);
// case a[i].first == a[j].first
for (int i = 0; i < n; i++) {
auto itr = as[a[i].first].upper_bound(a[i].second);
if (itr != as[a[i].first].end()) {
ans[i] = min(ans[i], *itr - a[i].second);
}
itr = as[a[i].first].find(a[i].second);
if (itr != as[a[i].first].begin()) {
itr = prev(itr);
ans[i] = min(ans[i], a[i].second - *itr);
}
}
// case abs(a[i].first - a[j].first) == 1
for (int i = 0; i < n; i++) {
if (as.find(a[i].first - 1) != as.end()) {
ans[i] = min(ans[i], max(a[i].second, *as[a[i].first - 1].begin()));
}
if (as.find(a[i].first + 1) != as.end()) {
ans[i] = min(ans[i], max(a[i].second, *as[a[i].first + 1].begin()));
}
}
// case abs(a[i].first - a[j].first) > 1
vector<int> iot(n);
iota(iot.begin(), iot.end(), 0);
sort(iot.begin(), iot.end(), [&](int i, int j){
return a[i] < a[j];
});
{// l to r
ll b = 1e10;
for (auto i : iot) {
ans[i] = min(ans[i], a[i].first + a[i].second - 1 + b);
b = min(b, a[i].second - 1 - a[i].first);
}
}
{// r to l
ll b = 1e10;
reverse(iot.begin(), iot.end());
for (auto i : iot) {
ans[i] = min(ans[i], -a[i].first + a[i].second - 1 + b);
b = min(b, a[i].second - 1 + a[i].first);
}
}
for(int i = 0; i < n; i++){
cout << ans[i] << endl;
}
}