Submission #198017

# Submission time Handle Problem Language Result Execution time Memory
198017 2020-01-24T13:49:10 Z wleung_bvg Drvca (COCI19_drvca) C++14
110 / 110
193 ms 7504 KB
#include <bits/stdc++.h>
using namespace std;
template<class C>constexpr int sz(const C&c){return int(c.size());}
using ll=long long;constexpr const char nl='\n',sp=' ';

const int MAXN = 1e5 + 5;

int N, H[MAXN];
map<int, int> vals, diffs;

void add(int v) {
    auto cur = vals.find(v);
    if (cur != vals.end()) {
        ++cur->second;
        ++diffs[0];
        return;
    }
    auto nxt = vals.upper_bound(v);
    if (nxt != vals.begin() && nxt != vals.end()) {
        auto it = diffs.find(nxt->first - prev(nxt)->first);
        if (--it->second == 0) diffs.erase(it);
    }
    cur = vals.emplace_hint(nxt, v, 1);
    if (nxt != vals.end()) ++diffs[nxt->first - v];
    if (cur != vals.begin()) ++diffs[v - prev(cur)->first];
}

void rem(int v) {
    auto cur = vals.find(v);
    if (cur->second >= 2) {
        --cur->second;
        auto it = diffs.find(0);
        if (--it->second == 0) diffs.erase(it);
        return;
    }
    auto nxt = next(cur);
    if (nxt != vals.end()) {
        auto it = diffs.find(nxt->first - v);
        if (--it->second == 0) diffs.erase(it);
    }
    if (cur != vals.begin()) {
        auto it = diffs.find(v - prev(cur)->first);
        if (--it->second == 0) diffs.erase(it);
    }
    vals.erase(cur);
    if (nxt != vals.begin()) if (nxt != vals.end()) ++diffs[nxt->first - prev(nxt)->first];
}

bool check(int A, int B) {
    int cur = A, diff = B - A;
    vector<int> reinsert;
    while (true) {
        auto it = vals.lower_bound(cur);
        if (it == vals.end() || it->first != cur) {
            break;
        }
        reinsert.push_back(it->first);
        rem(it->first);
        cur += diff;
        if (sz(diffs) <= 1) {
            vector<int> A, B;
            for (int v : reinsert) A.push_back(v);
            for (auto &&p : vals) for (int i = 0; i < p.second; i++) B.push_back(p.first);
            if (A.empty()) {
                A.push_back(B.back());
                B.pop_back();
            }
            if (B.empty()) {
                B.push_back(A.back());
                A.pop_back();
            }
            assert(sz(A) + sz(B) == N);
            cout << sz(A) << nl;
            for (int i = 0; i < sz(A); i++) cout << A[i] << " \n"[i == sz(A) - 1];
            cout << sz(B) << nl;
            for (int i = 0; i < sz(B); i++) cout << B[i] << " \n"[i == sz(B) - 1];
            return true;
        }
    }
    for (int v : reinsert) add(v);
    return false;
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // freopen("err.txt", "w", stderr);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> N;
    for (int i = 0; i < N; i++) cin >> H[i];
    sort(H, H + N);
    for (int i = 0; i < N; i++) add(H[i]);
    if (check(H[0], H[1])) return 0;
    if (N >= 3 && check(H[0], H[2])) return 0;
    if (N >= 3 && check(H[1], H[2])) return 0;
    cout << -1 << nl;
    return 0;
}
# 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 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 2 ms 376 KB Output is correct
14 Correct 2 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 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 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 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 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 380 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 7280 KB Output is correct
2 Correct 83 ms 6520 KB Output is correct
3 Correct 97 ms 7204 KB Output is correct
4 Correct 83 ms 6392 KB Output is correct
5 Correct 113 ms 7504 KB Output is correct
6 Correct 82 ms 6404 KB Output is correct
7 Correct 117 ms 7416 KB Output is correct
8 Correct 85 ms 6540 KB Output is correct
9 Correct 76 ms 5624 KB Output is correct
10 Correct 82 ms 6264 KB Output is correct
11 Correct 28 ms 2292 KB Output is correct
12 Correct 2 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 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 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 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 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 380 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 3 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 113 ms 7280 KB Output is correct
32 Correct 83 ms 6520 KB Output is correct
33 Correct 97 ms 7204 KB Output is correct
34 Correct 83 ms 6392 KB Output is correct
35 Correct 113 ms 7504 KB Output is correct
36 Correct 82 ms 6404 KB Output is correct
37 Correct 117 ms 7416 KB Output is correct
38 Correct 85 ms 6540 KB Output is correct
39 Correct 76 ms 5624 KB Output is correct
40 Correct 82 ms 6264 KB Output is correct
41 Correct 28 ms 2292 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 101 ms 6868 KB Output is correct
44 Correct 193 ms 6100 KB Output is correct
45 Correct 74 ms 6264 KB Output is correct
46 Correct 83 ms 6392 KB Output is correct
47 Correct 125 ms 5892 KB Output is correct
48 Correct 74 ms 6268 KB Output is correct
49 Correct 103 ms 6764 KB Output is correct
50 Correct 85 ms 5752 KB Output is correct
51 Correct 79 ms 6380 KB Output is correct
52 Correct 82 ms 6436 KB Output is correct
53 Correct 137 ms 6096 KB Output is correct
54 Correct 77 ms 6308 KB Output is correct
55 Correct 83 ms 6140 KB Output is correct
56 Correct 33 ms 2544 KB Output is correct
57 Correct 29 ms 2292 KB Output is correct
58 Correct 2 ms 376 KB Output is correct