結果

問題 No.2731 Two Colors
ユーザー rikein12rikein12
提出日時 2024-05-02 19:55:34
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 3,000 ms
コード長 4,868 bytes
コンパイル時間 3,089 ms
コンパイル使用メモリ 255,016 KB
実行使用メモリ 15,588 KB
最終ジャッジ日時 2024-11-23 14:01:45
合計ジャッジ時間 7,169 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 235 ms
11,240 KB
testcase_01 AC 234 ms
11,264 KB
testcase_02 AC 233 ms
11,236 KB
testcase_03 AC 4 ms
5,248 KB
testcase_04 AC 11 ms
5,632 KB
testcase_05 AC 9 ms
5,860 KB
testcase_06 AC 26 ms
6,092 KB
testcase_07 AC 29 ms
6,620 KB
testcase_08 AC 7 ms
5,248 KB
testcase_09 AC 98 ms
11,128 KB
testcase_10 AC 4 ms
5,248 KB
testcase_11 AC 116 ms
15,588 KB
testcase_12 AC 97 ms
11,184 KB
testcase_13 AC 97 ms
13,416 KB
testcase_14 AC 47 ms
10,368 KB
testcase_15 AC 6 ms
5,248 KB
testcase_16 AC 46 ms
8,120 KB
testcase_17 AC 62 ms
8,384 KB
testcase_18 AC 13 ms
5,248 KB
testcase_19 AC 49 ms
10,604 KB
testcase_20 AC 65 ms
9,320 KB
testcase_21 AC 16 ms
10,112 KB
testcase_22 AC 109 ms
10,980 KB
testcase_23 AC 30 ms
8,856 KB
testcase_24 AC 17 ms
7,296 KB
testcase_25 AC 4 ms
7,424 KB
testcase_26 AC 84 ms
9,496 KB
testcase_27 AC 106 ms
13,100 KB
testcase_28 AC 73 ms
9,440 KB
testcase_29 AC 24 ms
5,248 KB
testcase_30 AC 41 ms
8,960 KB
testcase_31 AC 30 ms
8,352 KB
testcase_32 AC 134 ms
11,352 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "lib/base/base.hpp"

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);++(i))
#define rep3(i,m,n) for(int (i)=(m);(i)<(n);++(i))
#define endl '\n'
#define all(v) (v).begin(),(v).end()

using ll = long long;

using vi = vector<int>;
using vvi = vector<vi>;

using vll = vector<ll>;
using vvll = vector<vll>;

template <class T>
using uset = unordered_set<T>;
template <class S, class T>
using umap = unordered_map<S, T>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T>
using gprique = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using lprique = priority_queue<T, vector<T>, less<T>>;

const int mod998 = 998244353;
const int mod107 = 1000000007;
#line 2 "lib/utility/io.hpp"

#define INT(...) \
  int __VA_ARGS__; \
  IN(__VA_ARGS__)
#define LL(...) \
  ll __VA_ARGS__; \
  IN(__VA_ARGS__)
#define STR(...) \
  string __VA_ARGS__; \
  IN(__VA_ARGS__)
#define CHR(...) \
  char __VA_ARGS__; \
  IN(__VA_ARGS__)
#define DBL(...) \
  long double __VA_ARGS__; \
  IN(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void read(int &a) { cin >> a; }
void read(long long &a) { cin >> a; }
void read(char &a) { cin >> a; }
void read(double &a) { cin >> a; }
void read(long double &a) { cin >> a; }
void read(string &a) { cin >> a; }
template <class T, class S> void read(pair<T, S> &p) { read(p.first), read(p.second); }
template <class T> void read(vector<T> &a) {for(auto &i : a) read(i);}
template <class T> void read(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  read(head);
  IN(tail...);
}

template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& A) {
  os << A.first << " " << A.second;
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& A) {
  for (size_t i = 0; i < A.size(); i++) {
    if(i) os << " ";
    os << A[i];
  }
  return os;
}

template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& A) {
  int count = 0;
  os << "{";
  for(auto [t, u] : A){
    if(count) os << ", ";
    os << t << ": " << u;
    count++;
  }
  os << "}";
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& A) {
  for(auto it = A.begin(); it != A.end(); ++it){
    if (it != A.begin()) os << " ";
    os << (*it);
  }
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& A) {
  size_t count = 0;
  T prev;
  os << "{";
  bool is_null = true, is_first = true;
  for(auto it = A.begin(); it != A.end(); ++it){
    if(it == A.begin()) {
      is_null = false;
      prev = (*it);
      count = 1;
    }
    if (prev != (*it)){
      if(!is_first) os << ", ";
      is_first = false;
      os << prev << ": " << count;
      prev = (*it);
      count = 1;
    } else {
      ++count;
    }
  }
  if(!is_null){
    if(!is_first) os << ", ";
    is_first = false;
    os << prev << ": " << count;
  }
  os << "}";
  return os;
}


void print() {
  cout << "\n";
  cout.flush();
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(Tail)) cout << " ";
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

int DP[1000][1000];
int A[1000][1000];

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h,w;
  cin >> h >> w;
  rep(i,h)rep(j,w)DP[i][j] = 0;
  DP[0][0] = 1;
  DP[h-1][w-1] = 2;
  rep(i,h)rep(j,w)cin>>A[i][j];
  gprique<tuple<int,int,int>> q[2];
  q[0].push({A[0][1],0,1});
  q[0].push({A[1][0],1,0});
  q[1].push({A[h-2][w-1],h-2,w-1});
  q[1].push({A[h-1][w-2],h-1,w-2});
  int i = 0;
  while (true){
    int turn = i%2;
    auto [a,x,y] = q[turn].top();
    while (DP[x][y]!=0){
      q[turn].pop();
      x = get<1>(q[turn].top());
      y = get<2>(q[turn].top());
    }//print(a,x,y,turn, DP[x][y]);
    q[turn].pop();
    DP[x][y] = turn+1;
    if (x>0){
      if (DP[x-1][y]==(1-turn)+1) break;
      else if (DP[x-1][y]==0) q[turn].push({A[x-1][y],x-1,y});
    } 
    if (x<h-1){
      if (DP[x+1][y]==(1-turn)+1) break;
      else if (DP[x+1][y]==0) q[turn].push({A[x+1][y],x+1,y});
    } 
    if (y>0){
      if (DP[x][y-1]==(1-turn)+1) break;
      else if (DP[x][y-1]==0) q[turn].push({A[x][y-1],x,y-1});
    } 
    if (y<w-1){
      if (DP[x][y+1]==(1-turn)+1) break;
      else if (DP[x][y+1]==0) q[turn].push({A[x][y+1],x,y+1});
    } 
    i++;
  }
  print(i+1);
}


0