結果

問題 No.2731 Two Colors
ユーザー MMMM
提出日時 2024-04-19 21:53:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,329 ms / 3,000 ms
コード長 2,031 bytes
コンパイル時間 4,422 ms
コンパイル使用メモリ 233,752 KB
実行使用メモリ 58,880 KB
最終ジャッジ日時 2024-04-19 21:53:41
合計ジャッジ時間 17,094 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,329 ms
58,752 KB
testcase_01 AC 1,320 ms
58,752 KB
testcase_02 AC 1,322 ms
58,880 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 31 ms
5,376 KB
testcase_05 AC 25 ms
5,376 KB
testcase_06 AC 96 ms
8,540 KB
testcase_07 AC 103 ms
7,648 KB
testcase_08 AC 15 ms
5,376 KB
testcase_09 AC 360 ms
17,256 KB
testcase_10 AC 9 ms
5,376 KB
testcase_11 AC 527 ms
27,536 KB
testcase_12 AC 347 ms
16,756 KB
testcase_13 AC 417 ms
23,472 KB
testcase_14 AC 158 ms
10,428 KB
testcase_15 AC 17 ms
5,376 KB
testcase_16 AC 178 ms
12,936 KB
testcase_17 AC 265 ms
14,480 KB
testcase_18 AC 45 ms
6,116 KB
testcase_19 AC 194 ms
13,604 KB
testcase_20 AC 227 ms
11,216 KB
testcase_21 AC 45 ms
6,144 KB
testcase_22 AC 432 ms
21,732 KB
testcase_23 AC 112 ms
8,924 KB
testcase_24 AC 52 ms
6,144 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 322 ms
16,152 KB
testcase_27 AC 420 ms
20,280 KB
testcase_28 AC 284 ms
17,772 KB
testcase_29 AC 90 ms
8,192 KB
testcase_30 AC 157 ms
10,348 KB
testcase_31 AC 103 ms
9,472 KB
testcase_32 AC 582 ms
28,600 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define chmin(x,y) (x) = min((x),(y))
#define chmax(x,y) (x) = max((x),(y))
#define ld long double
using namespace std;
using namespace atcoder;
using ll = long long;
const ll mod = 998244353;
using mint = modint998244353;
using Graph = vector<vector<pair<int,int>>>;
const vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1};

int main(){
  // input
  int H,W; cin >> H >> W;
  vector<vector<int>> A(H+2,vector<int>(W+2,2e9));
  for(int i = 1; i <= H; i++) for(int j = 1; j <= W; j++) cin >> A[i][j];
  
  // solve
  int ans = 0;
  vector<vector<int>> color(H+2,vector<int>(W+2,-1));
  set<pair<int,int>> ad_r, ad_b;
  priority_queue<vector<int>> nxt_r, nxt_b;
  
  color[1][1] = 1, color[H][W] = 0;
  ad_r.insert({1,2}); ad_r.insert({2,1});
  ad_b.insert({H-1,W}); ad_b.insert({H,W-1});
  nxt_r.push({-A[1][2],1,2});
  nxt_r.push({-A[2][1],2,1});
  nxt_b.push({-A[H-1][W],H-1,W});
  nxt_b.push({-A[H][W-1],H,W-1});
  
  bool adj = 0;
  while(!adj){
    ans++;
    if(ans % 2){
      int nxt_x = nxt_r.top()[1], nxt_y = nxt_r.top()[2];
      nxt_r.pop();
      color[nxt_x][nxt_y] = 1;
      if(ad_b.count({nxt_x,nxt_y})) adj = 1;
      else{
        for(int i = 0; i < 4; i++){
          int ad_x = nxt_x + dx[i], ad_y = nxt_y + dy[i];
          if(!ad_r.count({ad_x,ad_y}) && color[ad_x][ad_y] == -1){
            nxt_r.push({-A[ad_x][ad_y],ad_x,ad_y});
            ad_r.insert({ad_x,ad_y});
          }
        }
      }
    }
    else {
      int nxt_x = nxt_b.top()[1], nxt_y = nxt_b.top()[2];
      nxt_b.pop();
      color[nxt_x][nxt_y] = 0;
      if(ad_r.count({nxt_x,nxt_y})) adj = 1;
      else{
        for(int i = 0; i < 4; i++){
          int ad_x = nxt_x + dx[i], ad_y = nxt_y + dy[i];
          if(!ad_b.count({ad_x,ad_y}) && color[ad_x][ad_y] == -1){
            nxt_b.push({-A[ad_x][ad_y],ad_x,ad_y});
            ad_b.insert({ad_x,ad_y});
          }
          // nxt_b.push({-A[ad_x][ad_y],ad_x,ad_y});
        }
      }
    }
    
  }
  // output
  cout << ans << endl;
}
0