제출 #1273554

#제출 시각아이디문제언어결과실행 시간메모리
1273554chanhchuong123Drvca (COCI19_drvca)C++20
110 / 110
35 ms7800 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, int &cnt) { 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; bool magic = false; 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; magic = true; A.erase(it); last += df; --cnt; } } else { return false; } } if (last + df != h[i]) return false; } } last = h[i]; } if (magic) { while (A.size()) { auto it = A.lower_bound({last + df, 0}); if (it == A.end()) break; if ((*it).fi != last + df) break; flag[(*it).se] = 0; A.erase(it); --cnt; } } last = -1, df = -1; for (pair<int, int> pp: A) { if (last != -1) { 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, cnt)) { 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, cnt)) { 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, cnt)) { 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; }

컴파일 시 표준 에러 (stderr) 메시지

drvca.cpp: In function 'int main()':
drvca.cpp:160:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |                 freopen(task".inp", "r",  stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
drvca.cpp:161:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |                 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...