Cod sursă (job #770710)

Utilizator avatar S_Oleg Oleg Sirbu S_Oleg IP ascuns
Problemă Hex Compilator cpp-32 | 3,43 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 mar. 2024 13:49:57 Scor 100
#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};
string __fname = "hex"; ifstream in (__fname + ".in"); ofstream out (__fname + ".out"); 
#define cin in 
#define cout out
#define int64 long long
#define uint64 unsigned long long
#define x first
#define y second
#define pb push_back
#define pii pair <int, int> 
#define pii64 pair <int64, int64>
#define db(x) cout << "> " << #x << ": " << (x) << "\n"
#define qr queries()
#define yn(x) if (x) {ctn("Yes");}else {ctn("No");}
void solve(int);
void queries(){int n;cin >> n;for (int i = 1; i <= n; i++) solve(i);}
template<class T>T ceildiv(T a, T b) {return (a + b - 1) / b;}
template<class T>T gcd (T a, T b){return (b ? gcd(b, a % b): a);}
template<class T>T lcm (T a, T b){return a * b / gcd(a, b);}
// // // // // // // // // // // // // // // // // // // // // // 
/*                  TEMPLATE - VANILLA                         */
// // // // // // // // // // // // // // // // // // // // // //
const int ddx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx[] = {-1, -1, 0, 1, 1, 0};
const int dy[] = {0, 1, 1, 0, -1, -1};
const double pi = 3.14159265359;
const double eps = 1e-6;
const int64 hash_inv = 940594066;
const int64 hash_p = 101;
const int64 mod = 1e9 + 7;
const int maxn = 502;
int p[maxn * maxn];
bitset <maxn * maxn> top, bottom, l, r;
short col[maxn][maxn];
int n;
 
int getdad (int x) {
  return p[x] = (p[x] == x ? p[x] : getdad(p[x]));
}
 
void merge (int x, int y) {
  x = getdad(x), y = getdad(y);
  if (x == y) return;
  p[x] = y;
  if (top[x]) top[y] = 1;
  if (bottom[x]) bottom[y] = 1;
  if (l[x]) l[y] = 1;
  if (r[x]) r[y] = 1;
}
 
int hs (int x, int y) {
  return (x - 1) * n + y;
}
 
void solve(int id){
 
  return;
}
 
 
int main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed; cout << setprecision(10);
  cin >> n;
  int rs = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++){
      p[hs(i,j)] = hs(i, j);
    }
  }
  for (int i = 0; i < n * n; i++){
    int x,y;
    cin >> x >> y;
    if (rs) continue;
    col[x][y] = i % 2 + 1;;
    if (x == 1) top[getdad(hs(x,y))] = 1;
    if (x == n) bottom[getdad(hs(x,y))] = 1;
    if (y == 1) l[getdad(hs(x,y))] = 1;
    if (y == n) r[getdad(hs(x,y))] = 1;
    for (int k = 0; k < 6; k++){
      int nx = x + dx[k], ny = y + dy[k];
      if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && col[nx][ny] == col[x][y]) {
        merge(hs(x,y), hs(nx, ny));
      }
    }
    // cout << x << " " << y << " " << top[getdad(hs(x,y), i % 2)][i%2] << " " << bottom[getdad(hs(x,y), i % 2)][i%2] << "\n";
    if (i % 2 == 0 && l[getdad(hs(x,y))] && r[getdad(hs(x,y))]) {
      rs = i + 1;
    }
    if (i % 2 == 1 && top[getdad(hs(x,y))] && bottom[getdad(hs(x,y))]) {
      rs = i + 1;
    }
  }
  cout << rs;
  return 0;
}