Submission #674955

# Submission time Handle Problem Language Result Execution time Memory
674955 2022-12-26T16:59:20 Z LucaIlie Chessboard (IZhO18_chessboard) C++17
70 / 100
668 ms 5784 KB
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int maxK = 1e5;

struct rectangle {
    int l1, c1, l2, c2;
};

int l;
rectangle black[maxK];

int nxt( int x ) {
    if ( x % l == 0 )
        return x;
    return (x / l + 1) * l;
}

int prv( int x ) {
    return x / l * l;
}

long long wbSq( int lin, int col, bool isWhiteCorner ) {
    int x = (lin + 1) / 2, y = col / 2;
    return (x * x + y * y - 2 * x * y) * (isWhiteCorner ? 1 : -1);
}

long long getCost( rectangle r ) {
    int l1 = r.l1, c1 = r.c1, l2 = r.l2, c2 = r.c2;
    long long cost = 0;

    if ( l1 / l == l2 / l && c1 / l == c2 / l )
        return (l2 - l1 + 1) * (c2 - c1 + 1) * ((l1 / l + c1 / l) % 2 == 0 ? -1 : 1);

    if ( l1 % l != 0 && c1 % l != 0 )
        cost += (nxt( l1 ) - l1) * (nxt( c1 ) - c1) * wbSq( 1, 1, (l1 / l + c1 / l) % 2 );
    if ( l1 % l != 0 && c2 % l != 0 )
        cost += (nxt( l1 ) - l1) * (c2 - prv( c2 )) * wbSq( 1, 1, (l1 / l + c2 / l) % 2 );
    if ( l2 % l != 0 && c1 % l != 0 )
        cost += (l2 - prv( l2 )) * (nxt( c1 ) - c1) * wbSq( 1, 1, (l2 / l + c1 / l) % 2 );
    if ( l2 % l != 0 && c2 % l != 0 )
        cost += (l2 - prv( l2 )) * (c2 - prv( c2 )) * wbSq( 1, 1, (l2 / l + c2 / l) % 2 );

    if ( l1 % l != 0 )
        cost += (nxt( l1 ) - l1) * wbSq( 1, prv( c2 ) / l - nxt( c1 ) / l, !(l1 / l + nxt( c1 ) / l) % 2 );
    if ( c1 % l != 0 )
        cost += (nxt( c1 ) - c1) * wbSq( prv( l2 ) / l - nxt( c1 ) / l, 1, !(nxt( l1 ) / l + c1 / l) % 2 );
    if ( l2 % l != 0 )
        cost += (l2 - prv( l2 )) * wbSq( 1, prv( c2 ) / l - nxt( c1 ) / l, !(l2 / l + nxt( c1 ) / l) % 2 );
    if ( c2 % l != 0 )
        cost += (c2 - prv( c2 )) * wbSq( prv( l2 ) / l - nxt( c1 ) / l, 1, !(nxt( l1 ) / l + c2 / l) % 2 );

    cost += l * l * wbSq( prv( l2 ) / l - nxt( c1 ) / l, prv( c2 ) / l - nxt( c1 ) / l, (nxt( l1 ) / l + nxt( l2 ) / l) % 2 );

    printf( "%d %d %d %d %d: %d\n", l, l1, c1, l2, c2, cost );
    return cost;
}

signed main() {
    int n, k;
    long long cost, minCost;

    cin >> n >> k;
    for ( int i = 0; i < k; i++ ) {
        cin >> black[i].l1 >> black[i].c1 >> black[i].l2 >> black[i].c2;
        black[i].l1--;
        black[i].c1--;
        black[i].l2--;
        black[i].c2--;
    }

    minCost = n * n;
    for ( l = 1; l < n; l++ ) {
        if ( n % l == 0 ) {
            int x = (n / l + 1) / 2, y = n / l / 2;

            cost = (l * l) * (x * x + y * y);
            for ( int i = 0; i < k; i++ )
                cost += getCost( black[i] );
            minCost = min( minCost, cost );

            cost = 2 * x * y * l * l;
            for ( int i = 0; i < k; i++ )
                cost -= getCost( black[i] );
            minCost = min( minCost, cost );
        }
    }

    cout << minCost;

    return 0;
}

Compilation message

chessboard.cpp: In function 'long long int getCost(rectangle)':
chessboard.cpp:58:15: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
   58 |     printf( "%d %d %d %d %d: %d\n", l, l1, c1, l2, c2, cost );
      |              ~^                     ~
      |               |                     |
      |               int                   long long int
      |              %lld
chessboard.cpp:58:18: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long long int' [-Wformat=]
   58 |     printf( "%d %d %d %d %d: %d\n", l, l1, c1, l2, c2, cost );
      |                 ~^                     ~~
      |                  |                     |
      |                  int                   long long int
      |                 %lld
chessboard.cpp:58:21: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long long int' [-Wformat=]
   58 |     printf( "%d %d %d %d %d: %d\n", l, l1, c1, l2, c2, cost );
      |                    ~^                      ~~
      |                     |                      |
      |                     int                    long long int
      |                    %lld
chessboard.cpp:58:24: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long long int' [-Wformat=]
   58 |     printf( "%d %d %d %d %d: %d\n", l, l1, c1, l2, c2, cost );
      |                       ~^                       ~~
      |                        |                       |
      |                        int                     long long int
      |                       %lld
chessboard.cpp:58:27: warning: format '%d' expects argument of type 'int', but argument 6 has type 'long long int' [-Wformat=]
   58 |     printf( "%d %d %d %d %d: %d\n", l, l1, c1, l2, c2, cost );
      |                          ~^                        ~~
      |                           |                        |
      |                           int                      long long int
      |                          %lld
chessboard.cpp:58:31: warning: format '%d' expects argument of type 'int', but argument 7 has type 'long long int' [-Wformat=]
   58 |     printf( "%d %d %d %d %d: %d\n", l, l1, c1, l2, c2, cost );
      |                              ~^                        ~~~~
      |                               |                        |
      |                               int                      long long int
      |                              %lld
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 3688 KB Output is correct
2 Correct 17 ms 1108 KB Output is correct
3 Correct 41 ms 2344 KB Output is correct
4 Correct 41 ms 2504 KB Output is correct
5 Correct 64 ms 3168 KB Output is correct
6 Correct 36 ms 2188 KB Output is correct
7 Correct 8 ms 596 KB Output is correct
8 Correct 39 ms 2208 KB Output is correct
9 Correct 110 ms 5268 KB Output is correct
10 Correct 53 ms 3004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 316 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 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 316 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 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 26 ms 1484 KB Output is correct
17 Correct 72 ms 4196 KB Output is correct
18 Correct 87 ms 4904 KB Output is correct
19 Correct 175 ms 4428 KB Output is correct
20 Correct 180 ms 4892 KB Output is correct
21 Correct 66 ms 4068 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 45 ms 2252 KB Output is correct
24 Correct 82 ms 4484 KB Output is correct
25 Correct 10 ms 596 KB Output is correct
26 Correct 54 ms 2952 KB Output is correct
27 Correct 68 ms 3432 KB Output is correct
28 Correct 84 ms 4760 KB Output is correct
29 Correct 27 ms 1868 KB Output is correct
30 Correct 3 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 3688 KB Output is correct
2 Correct 17 ms 1108 KB Output is correct
3 Correct 41 ms 2344 KB Output is correct
4 Correct 41 ms 2504 KB Output is correct
5 Correct 64 ms 3168 KB Output is correct
6 Correct 36 ms 2188 KB Output is correct
7 Correct 8 ms 596 KB Output is correct
8 Correct 39 ms 2208 KB Output is correct
9 Correct 110 ms 5268 KB Output is correct
10 Correct 53 ms 3004 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 316 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 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 26 ms 1484 KB Output is correct
27 Correct 72 ms 4196 KB Output is correct
28 Correct 87 ms 4904 KB Output is correct
29 Correct 175 ms 4428 KB Output is correct
30 Correct 180 ms 4892 KB Output is correct
31 Correct 66 ms 4068 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 45 ms 2252 KB Output is correct
34 Correct 82 ms 4484 KB Output is correct
35 Correct 10 ms 596 KB Output is correct
36 Correct 54 ms 2952 KB Output is correct
37 Correct 68 ms 3432 KB Output is correct
38 Correct 84 ms 4760 KB Output is correct
39 Correct 27 ms 1868 KB Output is correct
40 Correct 3 ms 324 KB Output is correct
41 Correct 167 ms 4852 KB Output is correct
42 Correct 107 ms 5468 KB Output is correct
43 Correct 126 ms 4812 KB Output is correct
44 Correct 115 ms 5296 KB Output is correct
45 Correct 103 ms 5636 KB Output is correct
46 Correct 187 ms 5412 KB Output is correct
47 Correct 105 ms 5008 KB Output is correct
48 Correct 115 ms 5148 KB Output is correct
49 Correct 96 ms 4928 KB Output is correct
50 Correct 595 ms 5288 KB Output is correct
51 Correct 668 ms 5708 KB Output is correct
52 Correct 590 ms 5324 KB Output is correct
53 Correct 631 ms 5584 KB Output is correct
54 Correct 616 ms 5268 KB Output is correct
55 Correct 652 ms 5784 KB Output is correct
56 Correct 565 ms 5032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 68 ms 3688 KB Output is correct
10 Correct 17 ms 1108 KB Output is correct
11 Correct 41 ms 2344 KB Output is correct
12 Correct 41 ms 2504 KB Output is correct
13 Correct 64 ms 3168 KB Output is correct
14 Correct 36 ms 2188 KB Output is correct
15 Correct 8 ms 596 KB Output is correct
16 Correct 39 ms 2208 KB Output is correct
17 Correct 110 ms 5268 KB Output is correct
18 Correct 53 ms 3004 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 316 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 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 308 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 26 ms 1484 KB Output is correct
35 Correct 72 ms 4196 KB Output is correct
36 Correct 87 ms 4904 KB Output is correct
37 Correct 175 ms 4428 KB Output is correct
38 Correct 180 ms 4892 KB Output is correct
39 Correct 66 ms 4068 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 45 ms 2252 KB Output is correct
42 Correct 82 ms 4484 KB Output is correct
43 Correct 10 ms 596 KB Output is correct
44 Correct 54 ms 2952 KB Output is correct
45 Correct 68 ms 3432 KB Output is correct
46 Correct 84 ms 4760 KB Output is correct
47 Correct 27 ms 1868 KB Output is correct
48 Correct 3 ms 324 KB Output is correct
49 Correct 167 ms 4852 KB Output is correct
50 Correct 107 ms 5468 KB Output is correct
51 Correct 126 ms 4812 KB Output is correct
52 Correct 115 ms 5296 KB Output is correct
53 Correct 103 ms 5636 KB Output is correct
54 Correct 187 ms 5412 KB Output is correct
55 Correct 105 ms 5008 KB Output is correct
56 Correct 115 ms 5148 KB Output is correct
57 Correct 96 ms 4928 KB Output is correct
58 Correct 595 ms 5288 KB Output is correct
59 Correct 668 ms 5708 KB Output is correct
60 Correct 590 ms 5324 KB Output is correct
61 Correct 631 ms 5584 KB Output is correct
62 Correct 616 ms 5268 KB Output is correct
63 Correct 652 ms 5784 KB Output is correct
64 Correct 565 ms 5032 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Incorrect 0 ms 212 KB Output isn't correct
67 Halted 0 ms 0 KB -