답안 #168591

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
168591 2019-12-14T03:44:48 Z timothyhorsc Chessboard (IZhO18_chessboard) C++14
62 / 100
942 ms 8184 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i, l, r) for (int i=(l); i<(r); ++i)
#define RREP(i, r, l) for (int i=(r); i>(l); --i)
#define vi vector<int>
#define popcount __builtin_popcountll

const int INF = 1LL<<60;
const int MOD = 1000000007;

template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> t) {
    return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> t) {
    return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ")";
}

template <typename A>
string to_string(vector<A> v) {
    bool first = true;
    string result = "[";
    for (A i : v) {
        if (!first) result += ", ";
        first = false;
        result += to_string(i); 
    }
    return result + "]";
}

string bin_string(int x) {
    const int d = 64;
    string result(d, ' ');
    RREP(i, d-1, -1) result[d-1-i] = '0'+((x&1LL<<i)!=0);
    return result;
}

vector<int> getZ(string s) {
    int n = s.size();
    vector<int> z(n);
    int x = 0, y = 0;
    REP(i, 1, n) {
        z[i] = max(0LL, min(z[i-x], y-i+1));
        while (i+z[i] < n && s[z[i]] == s[i+z[i]]) {
            x = i;
            y = i+z[i];
            ++z[i];
        }
    }
    return z;
}

int powa(int base, int exp) {
    if (exp<=0) return 1;
    if (base<=0) return 0;
    int val = powa(base, exp>>1);
    val = val*val % MOD;
    if (exp&1) {
        val = val*base % MOD;
    }
    return val;
}

int n, k, res=INF;
vector<vi> blocks;

int bc(int d, int x1, int x2) {
    int firstb = x1/(2*d), lastb = x2/(2*d);
    int b = d*(lastb-firstb+1);
    firstb *= 2*d;
    lastb *= 2*d;
    b -= min(d, x1-firstb);
    b -= max(0LL, lastb+d-1-x2);
    return b;
}

bool getcost(int d) {
    int nd = n/d;
    int mincost = (nd*nd/2-k) * d*d;
    if (mincost >= res) return true;
    int cost = (nd*nd+1)/2 * d*d;
    for (vi block : blocks) {
        int x1=block[0],y1=block[1],x2=block[2],y2=block[3];
        --x1; --x2; --y1; --y2;
        int h = bc(d, x1, x2);
        int v = bc(d, y1, y2);
        int b = h*v + (x2-x1+1-h)*(y2-y1+1-v);
        int w = (x2-x1+1)*(y2-y1+1)-b;
        cost -= b;
        cost += w;
    }
    res = min(res, cost);
    res = min(res, n*n-cost);
    // cout << d << ": " << cost << ' ' << n*n-cost << '\n';
    return false;
}

main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n >> k;
    blocks.assign(k, vi(4));
    cin >> blocks;

    vi factors;
    for (int d=1; d*d<=n; ++d) {
        if (n%d==0) {
            factors.push_back(d);
            if (d*d!=n && d != 1) factors.push_back(n/d);
        }
    }
    sort(factors.rbegin(), factors.rend());
    for (int d : factors) {
        if (getcost(d)) break;
    }
    cout << res;
}

Compilation message

chessboard.cpp:109:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 5368 KB Output is correct
2 Correct 12 ms 1912 KB Output is correct
3 Correct 27 ms 3324 KB Output is correct
4 Correct 29 ms 4088 KB Output is correct
5 Correct 36 ms 4856 KB Output is correct
6 Correct 24 ms 3192 KB Output is correct
7 Correct 7 ms 1016 KB Output is correct
8 Correct 24 ms 3324 KB Output is correct
9 Correct 59 ms 7416 KB Output is correct
10 Correct 34 ms 4600 KB Output is correct
# 결과 실행 시간 메모리 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 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 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 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 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 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 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 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 19 ms 2680 KB Output is correct
17 Correct 50 ms 7032 KB Output is correct
18 Correct 75 ms 8056 KB Output is correct
19 Correct 267 ms 7160 KB Output is correct
20 Correct 297 ms 7928 KB Output is correct
21 Correct 46 ms 6648 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 49 ms 3784 KB Output is correct
24 Correct 71 ms 7416 KB Output is correct
25 Correct 11 ms 1144 KB Output is correct
26 Correct 43 ms 4856 KB Output is correct
27 Correct 65 ms 5756 KB Output is correct
28 Correct 72 ms 7672 KB Output is correct
29 Correct 20 ms 3064 KB Output is correct
30 Correct 4 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 5368 KB Output is correct
2 Correct 12 ms 1912 KB Output is correct
3 Correct 27 ms 3324 KB Output is correct
4 Correct 29 ms 4088 KB Output is correct
5 Correct 36 ms 4856 KB Output is correct
6 Correct 24 ms 3192 KB Output is correct
7 Correct 7 ms 1016 KB Output is correct
8 Correct 24 ms 3324 KB Output is correct
9 Correct 59 ms 7416 KB Output is correct
10 Correct 34 ms 4600 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 376 KB Output is correct
15 Correct 2 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 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 19 ms 2680 KB Output is correct
27 Correct 50 ms 7032 KB Output is correct
28 Correct 75 ms 8056 KB Output is correct
29 Correct 267 ms 7160 KB Output is correct
30 Correct 297 ms 7928 KB Output is correct
31 Correct 46 ms 6648 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 49 ms 3784 KB Output is correct
34 Correct 71 ms 7416 KB Output is correct
35 Correct 11 ms 1144 KB Output is correct
36 Correct 43 ms 4856 KB Output is correct
37 Correct 65 ms 5756 KB Output is correct
38 Correct 72 ms 7672 KB Output is correct
39 Correct 20 ms 3064 KB Output is correct
40 Correct 4 ms 504 KB Output is correct
41 Correct 155 ms 6776 KB Output is correct
42 Correct 69 ms 7560 KB Output is correct
43 Correct 93 ms 6776 KB Output is correct
44 Correct 58 ms 7416 KB Output is correct
45 Correct 65 ms 7800 KB Output is correct
46 Correct 219 ms 7544 KB Output is correct
47 Correct 58 ms 7032 KB Output is correct
48 Correct 83 ms 7160 KB Output is correct
49 Correct 57 ms 6904 KB Output is correct
50 Correct 865 ms 7388 KB Output is correct
51 Correct 906 ms 7800 KB Output is correct
52 Correct 840 ms 7288 KB Output is correct
53 Correct 894 ms 7800 KB Output is correct
54 Correct 824 ms 7160 KB Output is correct
55 Correct 942 ms 8184 KB Output is correct
56 Correct 799 ms 7032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -