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 pb push_back
#define all(a) begin(a), end(a)
using namespace std;
int n, k;
vector<int> v;
void load() {
cin >> n >> k;
v.resize(n + k);
for (auto &it : v) cin >> it;
}
int const N = 150005;
int ans[N];
bool solve(int suma) {
int l = 0, r = n + k - 1;
int cnt = 0;
int p = 0;
while (l < r) {
if (v[l] + v[r] == suma) {
ans[p++] = v[l];
ans[p++] = v[r];
if (p == n) return true;
++l;
--r;
} else if (v[l] + v[r] < suma) {
++l;
if (++cnt > k) return false;
} else {
--r;
if (++cnt > k) return false;
}
}
return false;
}
unordered_set<int> bio;
void solve() {
while (true) {
int i = rand() % (n + k);
int j = n + k - 1 - i + k - (rand() % (2 * k + 1));
if (i >= j || j >= n + k || bio.count(v[i] + v[j])) continue;
bio.insert(v[i] + v[j]);
if (solve(v[i] + v[j])) {
sort(ans, ans + n);
for (int i = 0; i < n; ++i) {
cout << ans[i] << ' ';
}
return;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
srand(chrono::steady_clock::now().time_since_epoch().count());
load();
sort(all(v));
solve();
return 0;
}
# | 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... |