답안 #609072

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
609072 2022-07-27T12:03:19 Z pakhomovee Chessboard (IZhO18_chessboard) C++17
70 / 100
745 ms 5692 KB
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <set>
#include <numeric>
#include <queue>
#include <functional>

using namespace std;

#define int long long

struct rect {
    int x1, y1, x2, y2;
    rect() {}
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<rect> r(k);
    int S = 0;
    for (int i = 0; i < k; ++i) {
        cin >> r[i].x1 >> r[i].y1 >> r[i].x2 >> r[i].y2;
        --r[i].x1; --r[i].y1; --r[i].x2; --r[i].y2;
        S += (r[i].x2 - r[i].x1 + 1) * (r[i].y2 - r[i].y1 + 1);
    }
    auto whiteCount = [&] (rect r, int len, int parity) {
        if (r.x1 / len == r.x2 / len && r.y1 / len == r.y2 / len) {
            int par = (r.x1 / len + r.y1 / len) % 2;
            if (par == parity) return (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1);
            return 0ll;
        } else if (r.x1 / len == r.x2 / len) {
            int firstFullColumn = (r.y1 + len - 1) / len;
            int lastFullColumn = r.y2 / len;
            int res = 0;
            if ((r.x1 / len + r.y1 / len) % 2 == parity) {
                res += (firstFullColumn * len - r.y1) * (r.x2 - r.x1 + 1);
            }
            if ((r.x1 / len + lastFullColumn) % 2 == parity) {
                res += (r.y2 - lastFullColumn * len + 1) * (r.x2 - r.x1 + 1);
            }
            int par = (r.x1 / len + firstFullColumn) % 2 == parity;
            res += (lastFullColumn - firstFullColumn + par) / 2 * (r.x2 - r.x1 + 1) * len;
            return res;
        } else if (r.y1 / len == r.y2 / len) {
            swap(r.x1, r.y1);
            swap(r.x2, r.y2);
            int firstFullColumn = (r.y1 + len - 1) / len;
            int lastFullColumn = r.y2 / len;
            int res = 0;
            if ((r.x1 / len + r.y1 / len) % 2 == parity) {
                res += (firstFullColumn * len - r.y1) * (r.x2 - r.x1 + 1);
            }
            if ((r.x1 / len + lastFullColumn) % 2 == parity) {
                res += (r.y2 - lastFullColumn * len + 1) * (r.x2 - r.x1 + 1);
            }
            int par = (r.x1 / len + firstFullColumn) % 2 == parity;
            res += (lastFullColumn - firstFullColumn + par) / 2 * (r.x2 - r.x1 + 1) * len;
            return res;
        }
        int firstFullColumn = (r.y1 + len - 1) / len;
        int lastFullColumn = r.y2 / len;
        int firstFullRow = (r.x1 + len - 1) / len;
        int lastFullRow = r.x2 / len;
        int fullBlockPar = (firstFullRow + firstFullColumn) % 2;
        int res = ((lastFullColumn - firstFullColumn) * (lastFullRow - firstFullRow) + (fullBlockPar == parity)) / 2 * len * len;
        //  add corners
        int corner1 = 0, corner2 = 0, corner3 = 0, corner4 = 0;
        int up, down, lt, rt;
        up = firstFullRow * len - r.x1;
        down = r.x2 - lastFullRow * len + 1;
        lt = firstFullColumn * len - r.y1;
        rt = r.y2 - lastFullColumn * len + 1;
        if ((firstFullRow + firstFullColumn) % 2 == parity) {
            corner1 = up * lt;
        }
        if ((firstFullRow + lastFullColumn + 1) % 2 == parity) {
            corner2 = up * rt;
        }
        if ((lastFullRow + lastFullColumn) % 2 == parity) {
            corner3 = down * rt;
        }
        if ((lastFullRow + firstFullColumn + 1) % 2 == parity) {
            corner4 = down * lt;
        }
        res += corner1 + corner2 + corner3 + corner4;
        //  add borders
        int ltb = 0, rtb = 0, upb = 0, downb = 0;
        int ltp = (firstFullRow + firstFullColumn + 1) % 2 == parity;
        int rtp = (firstFullRow + lastFullColumn + 1) % 2 == parity;
        int upp = (firstFullRow + 1 + firstFullColumn) % 2 == parity;
        int downp = (lastFullRow + 1 + firstFullColumn) % 2 == parity;
        ltb = (lastFullRow - firstFullRow + 1 + ltp) / 2 * lt * len;
        rtb = (lastFullRow - firstFullRow + 1 + rtp) / 2 * rt * len;
        upb = (lastFullColumn - firstFullColumn + 1 + upp) / 2 * up * len;
        downb = (lastFullColumn - firstFullColumn + 1 + downp) / 2 * down * len;
        res += ltb + rtb + upb + downb;
        return res;
    };
    int ans = n * n;
    for (int len = 1; len < n; ++len) {
        if (n % len) continue;
        if (1) {
            int cntSide = n / len;
            int black = (cntSide * cntSide + 1) / 2 * len * len;
            int wrongWhite = 0;
            for (rect q : r) {
                wrongWhite += whiteCount(q, len, 1);
            }
            ans = min(ans, wrongWhite * 2 + black - S);
        }
        if (1) {
            int cntSide = n / len;
            int black = (cntSide * cntSide) / 2 * len * len;
            int wrongWhite = 0;
            for (rect q : r) {
                wrongWhite += whiteCount(q, len, 0);
            }
            ans = min(ans, wrongWhite * 2 + black - S);
        }
    }
    cout << ans;
}
/*
 6 8
 3 3 3 3
 1 2 1 2
 3 4 3 4
 5 5 5 5
 4 3 4 3
 4 4 4 4
 2 1 2 1
 3 6 3 6
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 3732 KB Output is correct
2 Correct 10 ms 1108 KB Output is correct
3 Correct 17 ms 2388 KB Output is correct
4 Correct 20 ms 2596 KB Output is correct
5 Correct 21 ms 3296 KB Output is correct
6 Correct 14 ms 2232 KB Output is correct
7 Correct 4 ms 724 KB Output is correct
8 Correct 13 ms 2124 KB Output is correct
9 Correct 36 ms 5180 KB Output is correct
10 Correct 18 ms 3024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 13 ms 1540 KB Output is correct
17 Correct 36 ms 4188 KB Output is correct
18 Correct 40 ms 4988 KB Output is correct
19 Correct 142 ms 4452 KB Output is correct
20 Correct 166 ms 4992 KB Output is correct
21 Correct 24 ms 4168 KB Output is correct
22 Correct 1 ms 324 KB Output is correct
23 Correct 30 ms 2324 KB Output is correct
24 Correct 37 ms 4548 KB Output is correct
25 Correct 6 ms 724 KB Output is correct
26 Correct 24 ms 2900 KB Output is correct
27 Correct 48 ms 3524 KB Output is correct
28 Correct 37 ms 4812 KB Output is correct
29 Correct 15 ms 1876 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 3732 KB Output is correct
2 Correct 10 ms 1108 KB Output is correct
3 Correct 17 ms 2388 KB Output is correct
4 Correct 20 ms 2596 KB Output is correct
5 Correct 21 ms 3296 KB Output is correct
6 Correct 14 ms 2232 KB Output is correct
7 Correct 4 ms 724 KB Output is correct
8 Correct 13 ms 2124 KB Output is correct
9 Correct 36 ms 5180 KB Output is correct
10 Correct 18 ms 3024 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 320 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 13 ms 1540 KB Output is correct
27 Correct 36 ms 4188 KB Output is correct
28 Correct 40 ms 4988 KB Output is correct
29 Correct 142 ms 4452 KB Output is correct
30 Correct 166 ms 4992 KB Output is correct
31 Correct 24 ms 4168 KB Output is correct
32 Correct 1 ms 324 KB Output is correct
33 Correct 30 ms 2324 KB Output is correct
34 Correct 37 ms 4548 KB Output is correct
35 Correct 6 ms 724 KB Output is correct
36 Correct 24 ms 2900 KB Output is correct
37 Correct 48 ms 3524 KB Output is correct
38 Correct 37 ms 4812 KB Output is correct
39 Correct 15 ms 1876 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 134 ms 4792 KB Output is correct
42 Correct 45 ms 5492 KB Output is correct
43 Correct 101 ms 4828 KB Output is correct
44 Correct 44 ms 5272 KB Output is correct
45 Correct 51 ms 5584 KB Output is correct
46 Correct 154 ms 5324 KB Output is correct
47 Correct 35 ms 5020 KB Output is correct
48 Correct 83 ms 5100 KB Output is correct
49 Correct 45 ms 4856 KB Output is correct
50 Correct 745 ms 5288 KB Output is correct
51 Correct 745 ms 5636 KB Output is correct
52 Correct 661 ms 5160 KB Output is correct
53 Correct 702 ms 5580 KB Output is correct
54 Correct 607 ms 5128 KB Output is correct
55 Correct 700 ms 5692 KB Output is correct
56 Correct 620 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 23 ms 3732 KB Output is correct
10 Correct 10 ms 1108 KB Output is correct
11 Correct 17 ms 2388 KB Output is correct
12 Correct 20 ms 2596 KB Output is correct
13 Correct 21 ms 3296 KB Output is correct
14 Correct 14 ms 2232 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 13 ms 2124 KB Output is correct
17 Correct 36 ms 5180 KB Output is correct
18 Correct 18 ms 3024 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 316 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 13 ms 1540 KB Output is correct
35 Correct 36 ms 4188 KB Output is correct
36 Correct 40 ms 4988 KB Output is correct
37 Correct 142 ms 4452 KB Output is correct
38 Correct 166 ms 4992 KB Output is correct
39 Correct 24 ms 4168 KB Output is correct
40 Correct 1 ms 324 KB Output is correct
41 Correct 30 ms 2324 KB Output is correct
42 Correct 37 ms 4548 KB Output is correct
43 Correct 6 ms 724 KB Output is correct
44 Correct 24 ms 2900 KB Output is correct
45 Correct 48 ms 3524 KB Output is correct
46 Correct 37 ms 4812 KB Output is correct
47 Correct 15 ms 1876 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 134 ms 4792 KB Output is correct
50 Correct 45 ms 5492 KB Output is correct
51 Correct 101 ms 4828 KB Output is correct
52 Correct 44 ms 5272 KB Output is correct
53 Correct 51 ms 5584 KB Output is correct
54 Correct 154 ms 5324 KB Output is correct
55 Correct 35 ms 5020 KB Output is correct
56 Correct 83 ms 5100 KB Output is correct
57 Correct 45 ms 4856 KB Output is correct
58 Correct 745 ms 5288 KB Output is correct
59 Correct 745 ms 5636 KB Output is correct
60 Correct 661 ms 5160 KB Output is correct
61 Correct 702 ms 5580 KB Output is correct
62 Correct 607 ms 5128 KB Output is correct
63 Correct 700 ms 5692 KB Output is correct
64 Correct 620 ms 5068 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 0 ms 212 KB Output is correct
67 Incorrect 742 ms 5464 KB Output isn't correct
68 Halted 0 ms 0 KB -