This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(a) begin(a), end(a)
using namespace std;
int n, k;
vector<int> v;
vector<int> dif;
void load() {
cin >> n >> k;
v.resize(n + k);
for (auto &it : v) cin >> it;
}
map<pair<int, int>, pair<int, int>> nxt;
pair<int, int> getnxt(pair<int, int> const &p) {
if (p.F >= p.S) return p;
if (dif[p.F+1] != dif[p.S]) return p;
if (nxt.count(p)) return nxt[p];
return nxt[p] = getnxt({p.F+1, p.S-1});
}
vector<int> ans;
void getAns(int l, int r) {
if (l >= r) return;
ans.clear();
while (l < r) {
int cl = dif[l+1];
int cr = dif[r];
while (cl != cr && l < r) {
if (cl < cr) {
l++;
cl += dif[l+1];
} else {
r--;
cr += dif[r];
}
}
if (l >= r) break;
if (cl == cr) {
ans.pb(v[l]);
ans.pb(v[r]);
++l; --r;
}
if (ans.size() == n) return;
}
}
bool solve(int sl, int sr) {
int l = sl, r = sr;
if (l >= r) return false;
ans.clear();
ans.pb(v[l]);
ans.pb(v[r]);
int cnt = n + k - (sr - sl + 1);
while (l < r) {
tie(l, r) = getnxt({l, r});
int cl = dif[l+1];
int cr = dif[r];
while (cl != cr && l < r) {
if (cl < cr) {
l++;
cl += dif[l+1];
} else {
r--;
cr += dif[r];
}
if (++cnt > k) return false;
}
++l; --r;
}
getAns(sl, sr);
return true;
}
void solve() {
for (int i = 0; i <= k; ++i) {
for (int j = 0; j <= k - i; ++j) {
if (solve(i, n + k - 1 - j)) {
sort(all(ans));
for (auto &it: ans) cout << it << ' ';
return;
};
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
load();
sort(all(v));
dif.resize(size(v));
adjacent_difference(all(v), begin(dif));
solve();
return 0;
}
Compilation message (stderr)
tabletennis.cpp: In function 'void getAns(int, int)':
tabletennis.cpp:51:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if (ans.size() == n) return;
| ~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |