結果

問題 No.2743 Twisted Lattice
ユーザー butsurizukibutsurizuki
提出日時 2024-04-21 13:13:34
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,569 bytes
コンパイル時間 3,873 ms
コンパイル使用メモリ 295,540 KB
実行使用メモリ 83,100 KB
最終ジャッジ日時 2024-04-21 13:13:46
合計ジャッジ時間 10,278 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 659 ms
82,816 KB
testcase_01 WA -
testcase_02 AC 256 ms
26,668 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using pl=pair<long long,long long>;

long long ds(pl a,pl b){
  if(abs(a.second-b.second)<=1){
    return abs(a.first-b.first)+abs(a.second-b.second)*min(a.first,b.first);
  }
  return a.first+b.first+abs(a.second-b.second);
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long h,w,n;
  cin >> h >> w >> n;
  map<long long,set<pl>> mp;
  vector<long long> x(n),y(n);
  vector<pl> p(n);

  for(long long i=0;i<n;i++){
    cin >> x[i] >> y[i];
    p[i]={x[i],y[i]};
    mp[y[i]].insert({x[i],i});
  }

  vector<long long> res(n,8e18);
  for(long long i=0;i<n;i++){
    for(long long d=-1;d<=1;d++){
      long long cy=y[i]+d;
      if(mp[cy].empty()){continue;}
      auto it=mp[cy].lower_bound({x[i],i+1});

      if(it!=mp[cy].end()){
        res[i]=min(res[i],ds(p[i],p[(*it).second]));
      }
      it--;
      if(it!=mp[cy].begin()){
        it--;
        res[i]=min(res[i],ds(p[i],p[(*it).second]));
      }
    }
  }

  // for(auto &nx : res){
  //   cout << nx << "\n";
  // }

  vector<pl> vp(n);
  for(long long i=0;i<n;i++){
    vp[i]={y[i],i};
  }
  sort(vp.begin(),vp.end());

  long long md=4e18;
  for(auto &nx : vp){
    res[nx.second]=min(res[nx.second],md+x[nx.second]+y[nx.second]-2);
    md=min(md,x[nx.second]-y[nx.second]);
  }

  md=4e18;
  reverse(vp.begin(),vp.end());
  for(auto &nx : vp){
    res[nx.second]=min(res[nx.second],md+x[nx.second]-y[nx.second]-2);
    md=min(md,x[nx.second]+y[nx.second]);
  }

  for(auto &nx : res){
    cout << nx << "\n";
  }
  return 0;
}
0