답안 #677769

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
677769 2023-01-04T10:50:20 Z vjudge1 Chessboard (IZhO18_chessboard) C++17
39 / 100
472 ms 5004 KB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t
#define x1 x1_
#define x2 x2_
#define y1 y1_
#define y2 y2_

const int N = 1e5 + 5;
const int BLACK_FIRST = 0;
const int WHITE_FIRST = 1;

// color of a ceil will be determined by (id row + id col) % 2 and the color of first block (BLACK_FIRST / WHITE_FIRST)
// in particular: (id row + id col) % 2 = BLACK_FIRST --> black id, otherwise ...
// id row = row div edge

int n, k;
int x[2][N], y[2][N];

void read() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> x[j][i] >> y[j][i];
      --x[j][i];
      --y[j][i];
    }
  }
}

int id_block (int x, int edge) {
  return x / edge;
}

// (0...x1) / k % 2 == type
int num_ceil (int x, int k, int type) {
  if (x < 0) {
    return 0;
  }
  if (x == 0) {
    return (x / k) % 2 == type;
  }
  int block_x = id_block(x, k);
  int ans = 0;

  if (block_x > 0) {
    // [0, block_x - 1]
    if (type == 0) {
      ans += (block_x - block_x / 2) * k;
    }
    else {
      ans += (block_x / 2) * k;
    }
  }

  // just in block_x
  int lo = block_x * k;
  int hi = x;

  if (block_x % 2 == type) {
    ans += (hi - lo + 1);
  }

  return ans;
}

int num_ceil (int l, int r, int k, int type) {
  assert(num_ceil(r, k, type) >= num_ceil(l - 1, k, type));
  return num_ceil(r, k, type) - num_ceil(l - 1, k, type);
}

int solve (int edge, int type) {
  int ans = 0;

  // (r + c) % 2 == type
  for (int row_type = 0; row_type < 2; row_type++) {
    int col_type = (type - row_type + 2) % 2;
    ans += num_ceil(0, n - 1, edge, row_type) * num_ceil(0, n - 1, edge, col_type);
  }

  for (int i = 1; i <= k; i++) {
    for (int row_type = 0; row_type < 2; row_type++) {
      for (int col_type = 0; col_type < 2; col_type++) {
        // already black
        // since we colored it before --> subtract
        if ((row_type + col_type) % 2 == type) {
          ans -= num_ceil(x[0][i], x[1][i], edge, row_type) * num_ceil(y[0][i], y[1][i], edge, col_type);
        }
        // must be white, but now it's black
        else {
          ans += num_ceil(x[0][i], x[1][i], edge, row_type) * num_ceil(y[0][i], y[1][i], edge, col_type);
        }
      }
    }
  }

  return ans;
}

void solve() {
  int best = 2e9;
  for (int edge = 1; edge < n; edge++) {
    if (n % edge) {
      continue;
    }
    best = min(best, solve(edge, BLACK_FIRST));
    best = min(best, solve(edge, WHITE_FIRST));
  }
  cout << best;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

  read();
  solve();

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 3772 KB Output is correct
2 Correct 11 ms 1108 KB Output is correct
3 Incorrect 23 ms 2428 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 22 ms 1552 KB Output is correct
17 Correct 36 ms 4308 KB Output is correct
18 Correct 77 ms 4960 KB Output is correct
19 Correct 449 ms 4536 KB Output is correct
20 Correct 472 ms 5004 KB Output is correct
21 Correct 36 ms 4168 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 63 ms 2364 KB Output is correct
24 Correct 69 ms 4600 KB Output is correct
25 Correct 13 ms 724 KB Output is correct
26 Correct 45 ms 3008 KB Output is correct
27 Correct 76 ms 3544 KB Output is correct
28 Correct 74 ms 4816 KB Output is correct
29 Correct 14 ms 1880 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 3772 KB Output is correct
2 Correct 11 ms 1108 KB Output is correct
3 Incorrect 23 ms 2428 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 35 ms 3772 KB Output is correct
10 Correct 11 ms 1108 KB Output is correct
11 Incorrect 23 ms 2428 KB Output isn't correct
12 Halted 0 ms 0 KB -