Submission #1273545

#TimeUsernameProblemLanguageResultExecution timeMemory
1273545chanhchuong123Drvca (COCI19_drvca)C++20
0 / 110
1095 ms7720 KiB
#include<bits/stdc++.h>
using namespace std;

const bool multiTest = false;
#define task "C"
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)

template<typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) a = b; else return 0; return 1;
}
template<typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) a = b; else return 0; return 1;
}

const int MAX = 100010;
int n, h[MAX];

bool checkValid(vector<bool> &flag) {
    multiset<pair<int, int>> A;
    for (int i = 1; i <= n; ++i) if (flag[i]) {
        A.insert({h[i], i});
    }

    int last = -1, df = -1;
    for (int i = 1; i <= n; ++i) if (!flag[i]) {
        if (last != -1) {
            if (df == -1) df = h[i] - last; else
            if (last + df > h[i]) return false;
            else {
                while (last + df < h[i]) {
                    auto it = A.lower_bound({last + df, 0});
                    if (it != A.end()) {
                        if ((*it).fi != last + df) return false;
                        else {
                            flag[(*it).se] = 0;
                            A.erase(it);
                            last += df;
                        }
                    }
                }
                if (last + df != h[i]) return false;
            }
        }
        last = h[i];
    }

    last = -1, df = -1;
    for (pair<int, int> pp: A) {
        if (last != -1) last = pp.se;
        else {
            if (df == -1) df = pp.fi - last; else
            if (pp.fi - last != df) return false;
        }
        last = pp.fi;
    }
    return true;
}

void process(void) {
    cin >> n; for (int i = 1; i <= n; ++i) cin >> h[i];
    sort(h + 1, h + 1 + n);

    { // Case 1: A = {h[1], h[2], ...}, B = {...}
        vector<bool> flag(n + 1, false);
        int df = h[2] - h[1];
        flag[1] = 1;
        int pos = 1;
        int cnt = 1;
        while (pos < n) {
            int nextPos = lower_bound(h + 1 + pos, h + 1 + n, h[pos] + df) - h;
            if (h[nextPos] == h[pos] + df) {
                pos = nextPos;
                flag[pos] = 1;
                ++cnt;
            } else {
                break;
            }
        }
        if (cnt == n) flag[n] = false, --cnt;
        if (checkValid(flag)) {
            cout << cnt << '\n'; for (int i = 1; i <= n; ++i) if (flag[i]) cout << h[i] << ' '; cout << '\n';
            cout << n - cnt << '\n'; for (int i = 1; i <= n; ++i) if (!flag[i]) cout << h[i] << ' ';
            return;
        }
    }

    { // Case 2: A = {h[1], h[3], ...}, B = {h[2], }
        vector<bool> flag(n + 1, false);
        int df = h[3] - h[1];
        flag[1] = 1;
        int pos = 1;
        int cnt = 1;
        while (pos < n) {
            int nextPos = lower_bound(h + 1 + pos, h + 1 + n, h[pos] + df) - h;
            if (h[nextPos] == h[pos] + df) {
                pos = nextPos;
                flag[pos] = 1;
                ++cnt;
            } else {
                break;
            }
        }
        if (cnt == n) flag[n] = false, --cnt;
        if (checkValid(flag)) {
            cout << cnt << '\n'; for (int i = 1; i <= n; ++i) if (flag[i]) cout << h[i] << ' '; cout << '\n';
            cout << n - cnt << '\n'; for (int i = 1; i <= n; ++i) if (!flag[i]) cout << h[i] << ' ';
            return;
        }
    }

    { // Case 3: A = {h[2], h[3], ...}, B = {h[1], }
        vector<bool> flag(n + 1, false);
        int df = h[3] - h[2];
        flag[2] = 1;
        int pos = 2;
        int cnt = 1;
        while (pos < n) {
            int nextPos = lower_bound(h + 1 + pos, h + 1 + n, h[pos] + df) - h;
            if (h[nextPos] == h[pos] + df) {
                pos = nextPos;
                flag[pos] = 1;
                ++cnt;
            } else {
                break;
            }
        }
        if (checkValid(flag)) {
            cout << cnt << '\n'; for (int i = 1; i <= n; ++i) if (flag[i]) cout << h[i] << ' '; cout << '\n';
            cout << n - cnt << '\n'; for (int i = 1; i <= n; ++i) if (!flag[i]) cout << h[i] << ' ';
            return;
        }
    }

    cout << -1;
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r",  stdin);
		freopen(task".out", "w", stdout);
	}

	int nTest = 1; if (multiTest) cin >> nTest;
	while (nTest--) {
		process();
	}

	return 0;
}

Compilation message (stderr)

drvca.cpp: In function 'int main()':
drvca.cpp:145:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |                 freopen(task".inp", "r",  stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
drvca.cpp:146:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |                 freopen(task".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...