Submission #168971

# Submission time Handle Problem Language Result Execution time Memory
168971 2019-12-17T10:32:52 Z cormac Chessboard (IZhO18_chessboard) C++14
100 / 100
1271 ms 6672 KB
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)

using ll = long long;
using bk = tuple<ll, ll, ll, ll, ll>;

ll INF = 1LL << 60;

ll count1d(ll x, ll w, ll k) {
    ll sx = k * ((x / k) + (x % k != 0));
    ll ex = (x + w) / k * k;
    if (ex < sx) return w;
    ll m = (ex - sx);
    return (sx - x) + (x + w - ex) * ((sx ==x) != (m/k%2==1)) + (m / k / 2) * k + k * ((m/k)%2 && (sx == x));
}

ll count2d(ll x, ll y, ll w, ll h, ll k) {
    ll r = count1d(x, w, k); 
    if ((x / k + y / k) % 2 == 1) r = w - r;
    ll c = count1d(y, h, k);
    return  r * c + (w-r) * (h-c);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ll N, K, A, tA;
    cin >> N >> K;
    A = N*N;
    
    
    vector<bk> blocks (K);
    F0R(i, K) {
        ll x, y, w, h;
        cin >> x >> y >> w >> h;
        w-=x; h-=y;
        --x; --y; ++w; ++h; tA += w * h;
        blocks[i] = make_tuple(w*h, y, x, h, w);
    }

    sort(blocks.begin(), blocks.end());

    ll best = INF;

    ROF(i, 1, N) {
        if (N % i != 0) continue;

        ll cA = tA;
        
        ll c1 = count2d(0, 0, N, N, i);
        ll c2 = A - c1;

        for (bk block : blocks) {
            ll x, y, w, h, a;
            tie(a, x, y, w, h) = block;    
            ll overlap = count2d(x, y, w, h, i);
            cA -= a;
            c1 -= overlap;
            c1 += a - overlap;
            c2 += overlap;
            c2 -= a - overlap;
            if (c2 - cA >= best && c1 - cA >= best) break;
        }
        best = min(best, c1);
        best = min(best, c2);
    }
    
    cout << best << endl; 
}

Compilation message

chessboard.cpp: In function 'int main()':
chessboard.cpp:61:16: warning: 'tA' may be used uninitialized in this function [-Wmaybe-uninitialized]
             cA -= a;
             ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 4344 KB Output is correct
2 Correct 13 ms 1404 KB Output is correct
3 Correct 30 ms 2808 KB Output is correct
4 Correct 33 ms 3064 KB Output is correct
5 Correct 41 ms 3832 KB Output is correct
6 Correct 27 ms 2552 KB Output is correct
7 Correct 8 ms 760 KB Output is correct
8 Correct 27 ms 2524 KB Output is correct
9 Correct 67 ms 6008 KB Output is correct
10 Correct 38 ms 3576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 380 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 380 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 24 ms 1784 KB Output is correct
17 Correct 55 ms 4984 KB Output is correct
18 Correct 84 ms 5752 KB Output is correct
19 Correct 270 ms 5368 KB Output is correct
20 Correct 301 ms 5956 KB Output is correct
21 Correct 54 ms 4856 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 53 ms 2808 KB Output is correct
24 Correct 76 ms 5368 KB Output is correct
25 Correct 12 ms 888 KB Output is correct
26 Correct 49 ms 3448 KB Output is correct
27 Correct 73 ms 4088 KB Output is correct
28 Correct 83 ms 5748 KB Output is correct
29 Correct 23 ms 2168 KB Output is correct
30 Correct 4 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 4344 KB Output is correct
2 Correct 13 ms 1404 KB Output is correct
3 Correct 30 ms 2808 KB Output is correct
4 Correct 33 ms 3064 KB Output is correct
5 Correct 41 ms 3832 KB Output is correct
6 Correct 27 ms 2552 KB Output is correct
7 Correct 8 ms 760 KB Output is correct
8 Correct 27 ms 2524 KB Output is correct
9 Correct 67 ms 6008 KB Output is correct
10 Correct 38 ms 3576 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 3 ms 380 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 24 ms 1784 KB Output is correct
27 Correct 55 ms 4984 KB Output is correct
28 Correct 84 ms 5752 KB Output is correct
29 Correct 270 ms 5368 KB Output is correct
30 Correct 301 ms 5956 KB Output is correct
31 Correct 54 ms 4856 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 53 ms 2808 KB Output is correct
34 Correct 76 ms 5368 KB Output is correct
35 Correct 12 ms 888 KB Output is correct
36 Correct 49 ms 3448 KB Output is correct
37 Correct 73 ms 4088 KB Output is correct
38 Correct 83 ms 5748 KB Output is correct
39 Correct 23 ms 2168 KB Output is correct
40 Correct 4 ms 504 KB Output is correct
41 Correct 64 ms 5612 KB Output is correct
42 Correct 65 ms 6344 KB Output is correct
43 Correct 57 ms 5624 KB Output is correct
44 Correct 63 ms 6256 KB Output is correct
45 Correct 71 ms 6560 KB Output is correct
46 Correct 80 ms 6264 KB Output is correct
47 Correct 65 ms 5808 KB Output is correct
48 Correct 60 ms 5880 KB Output is correct
49 Correct 58 ms 5624 KB Output is correct
50 Correct 71 ms 6124 KB Output is correct
51 Correct 75 ms 6392 KB Output is correct
52 Correct 70 ms 6136 KB Output is correct
53 Correct 75 ms 6392 KB Output is correct
54 Correct 69 ms 5900 KB Output is correct
55 Correct 78 ms 6648 KB Output is correct
56 Correct 67 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 46 ms 4344 KB Output is correct
10 Correct 13 ms 1404 KB Output is correct
11 Correct 30 ms 2808 KB Output is correct
12 Correct 33 ms 3064 KB Output is correct
13 Correct 41 ms 3832 KB Output is correct
14 Correct 27 ms 2552 KB Output is correct
15 Correct 8 ms 760 KB Output is correct
16 Correct 27 ms 2524 KB Output is correct
17 Correct 67 ms 6008 KB Output is correct
18 Correct 38 ms 3576 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 3 ms 380 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 24 ms 1784 KB Output is correct
35 Correct 55 ms 4984 KB Output is correct
36 Correct 84 ms 5752 KB Output is correct
37 Correct 270 ms 5368 KB Output is correct
38 Correct 301 ms 5956 KB Output is correct
39 Correct 54 ms 4856 KB Output is correct
40 Correct 3 ms 376 KB Output is correct
41 Correct 53 ms 2808 KB Output is correct
42 Correct 76 ms 5368 KB Output is correct
43 Correct 12 ms 888 KB Output is correct
44 Correct 49 ms 3448 KB Output is correct
45 Correct 73 ms 4088 KB Output is correct
46 Correct 83 ms 5748 KB Output is correct
47 Correct 23 ms 2168 KB Output is correct
48 Correct 4 ms 504 KB Output is correct
49 Correct 64 ms 5612 KB Output is correct
50 Correct 65 ms 6344 KB Output is correct
51 Correct 57 ms 5624 KB Output is correct
52 Correct 63 ms 6256 KB Output is correct
53 Correct 71 ms 6560 KB Output is correct
54 Correct 80 ms 6264 KB Output is correct
55 Correct 65 ms 5808 KB Output is correct
56 Correct 60 ms 5880 KB Output is correct
57 Correct 58 ms 5624 KB Output is correct
58 Correct 71 ms 6124 KB Output is correct
59 Correct 75 ms 6392 KB Output is correct
60 Correct 70 ms 6136 KB Output is correct
61 Correct 75 ms 6392 KB Output is correct
62 Correct 69 ms 5900 KB Output is correct
63 Correct 78 ms 6648 KB Output is correct
64 Correct 67 ms 5752 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 376 KB Output is correct
67 Correct 1188 ms 6280 KB Output is correct
68 Correct 1182 ms 6136 KB Output is correct
69 Correct 1020 ms 5496 KB Output is correct
70 Correct 1113 ms 6136 KB Output is correct
71 Correct 1130 ms 5988 KB Output is correct
72 Correct 1088 ms 5880 KB Output is correct
73 Correct 1080 ms 5880 KB Output is correct
74 Correct 1186 ms 6264 KB Output is correct
75 Correct 1107 ms 5936 KB Output is correct
76 Correct 1195 ms 6392 KB Output is correct
77 Correct 205 ms 6648 KB Output is correct
78 Correct 85 ms 6008 KB Output is correct
79 Correct 148 ms 5724 KB Output is correct
80 Correct 152 ms 6008 KB Output is correct
81 Correct 143 ms 5624 KB Output is correct
82 Correct 125 ms 6316 KB Output is correct
83 Correct 99 ms 5832 KB Output is correct
84 Correct 715 ms 6580 KB Output is correct
85 Correct 1254 ms 6648 KB Output is correct
86 Correct 4 ms 380 KB Output is correct
87 Correct 3 ms 376 KB Output is correct
88 Correct 1271 ms 6672 KB Output is correct
89 Correct 248 ms 1532 KB Output is correct
90 Correct 3 ms 376 KB Output is correct