結果

問題 No.2731 Two Colors
ユーザー Nzt3Nzt3
提出日時 2024-04-20 02:00:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 1,761 bytes
コンパイル時間 3,101 ms
コンパイル使用メモリ 209,384 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-20 02:00:12
合計ジャッジ時間 6,795 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 202 ms
11,520 KB
testcase_01 AC 208 ms
11,520 KB
testcase_02 AC 210 ms
11,520 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 9 ms
6,940 KB
testcase_05 AC 7 ms
6,940 KB
testcase_06 AC 25 ms
6,940 KB
testcase_07 AC 28 ms
6,944 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 100 ms
9,312 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 100 ms
8,568 KB
testcase_12 AC 98 ms
8,960 KB
testcase_13 AC 86 ms
7,728 KB
testcase_14 AC 41 ms
6,940 KB
testcase_15 AC 4 ms
6,940 KB
testcase_16 AC 41 ms
6,940 KB
testcase_17 AC 58 ms
6,940 KB
testcase_18 AC 11 ms
6,940 KB
testcase_19 AC 39 ms
6,944 KB
testcase_20 AC 65 ms
7,168 KB
testcase_21 AC 13 ms
6,940 KB
testcase_22 AC 105 ms
9,344 KB
testcase_23 AC 24 ms
6,944 KB
testcase_24 AC 13 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 84 ms
8,192 KB
testcase_27 AC 105 ms
9,444 KB
testcase_28 AC 66 ms
7,368 KB
testcase_29 AC 19 ms
6,944 KB
testcase_30 AC 39 ms
6,940 KB
testcase_31 AC 26 ms
6,944 KB
testcase_32 AC 122 ms
10,204 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr int MOD=998244353;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H,W;
  cin>>H>>W;
  vector A(H,vector(W,0));
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin>>A[i][j];
    }
  }
  vector used0(H,vector(W,false)),used1=used0;
  vector colored(H,vector(W,0));
  using a3i=array<int,3>;
  priority_queue<a3i,vector<a3i>,greater<a3i>>pq0,pq1;
  pq0.push({0,0,0});
  used0[0][0]=1;
  pq1.push({0,H-1,W-1});
  used1[H-1][W-1]=1;
  int ans=-2;
  int dx[]={0,1,0,-1};
  int dy[]={-1,0,1,0};
  auto bound_ok=[&](int x,int y){
    return x>=0&&x<H &&y>=0&&y<W;
  };
  while(1){
    {
      while(pq0.size()&&colored[pq0.top()[1]][pq0.top()[2]]){
        pq0.pop();
      }
      if(pq0.empty())break;
      auto [d,i,j]=pq0.top();
      colored[i][j]=1;
      ans+=1;
      pq0.pop();
      bool ng=0;
      for(int k=0;k<4;k++){
        int i2=i+dx[k],j2=j+dy[k];
        if(bound_ok(i2,j2)&&colored[i2][j2]==2){
          ng=1;
          break;
        }else if(bound_ok(i2,j2)&&!used0[i2][j2]){
          used0[i2][j2]=1;
          pq0.push({A[i2][j2],i2,j2});
        }
      }
      if(ng)break;
    }
    {
      while(pq1.size()&&colored[pq1.top()[1]][pq1.top()[2]]){
        pq1.pop();
      }
      if(pq1.empty())break;
      auto [d,i,j]=pq1.top();
      colored[i][j]=2;
      ans+=1;
      pq1.pop();
      bool ng=0;
      for(int k=0;k<4;k++){
        int i2=i+dx[k],j2=j+dy[k];
        if(bound_ok(i2,j2)&&colored[i2][j2]==1){
          ng=1;
          break;
        }else if(bound_ok(i2,j2)&&!used1[i2][j2]){
          used1[i2][j2]=1;
          pq1.push({A[i2][j2],i2,j2});
        }
      }
      if(ng)break;
    }
  }
  cout<<ans<<'\n';
}
0