結果
| 問題 |
No.2743 Twisted Lattice
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-20 02:42:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 610 ms / 3,000 ms |
| コード長 | 1,345 bytes |
| コンパイル時間 | 2,639 ms |
| コンパイル使用メモリ | 216,296 KB |
| 最終ジャッジ日時 | 2025-02-21 06:01:17 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
int main(){
int h,w,n;
cin>>h>>w>>n;
vector<int> a(n),b(n);
rep(i,n) cin>>a.at(i)>>b.at(i);
vector<ll> ans(n,1e18);
map<int,vector<pair<ll,ll>>> mp;
rep(i,n) mp[b.at(i)].push_back({a.at(i),i});
for(auto&[k,v]:mp){
sort(v.begin(),v.end());
int m=v.size();
rep(i,m){
if(i>0) ans.at(v.at(i).second)=min(ans.at(v.at(i).second),v.at(i).first-v.at(i-1).first);
if(i+1<m) ans.at(v.at(i).second)=min(ans.at(v.at(i).second),v.at(i+1).first-v.at(i).first);
}
}
for(auto&[k,v]:mp){
for(int shf:{-1,1}){
if(mp.count(k-shf)){
auto[mn,j]=mp.at(k-shf).front();
for(auto[p,i]:v){
ll tmp=0;
tmp+=min(mn,p);
tmp+=abs(mn-p);
ans.at(i)=min(ans.at(i),tmp);
}
}
}
}
vector<pair<int,ll>> vp;
for(auto&[k,v]:mp){
vp.push_back({k,v.front().first});
}
int m=vp.size();
vector<ll> rmn(m,1e18),lmn(m,1e18);
rep(i,m-1){
lmn.at(i+1)=min(lmn.at(i),vp.at(i).second)+vp.at(i+1).first-vp.at(i).first;
}
for(int i=m-1;i>0;i--){
rmn.at(i-1)=min(rmn.at(i),vp.at(i).second)+vp.at(i).first-vp.at(i-1).first;
}
rep(i,m){
auto &v=mp.at(vp.at(i).first);
ll tmp=min(lmn.at(i),rmn.at(i));
for(auto[p,j]:v){
ans.at(j)=min(ans.at(j),tmp+p-2);
}
}
rep(i,n) cout<<ans.at(i)<<"\n";
}