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>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<ll, ll>
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define pb push_back
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 2e5+5;
int a[maxn];
int n, k;
unordered_set<int> tried;
void get(int sum);
bool test(int sum) {
if (tried.count(sum)) return 0;
int lp = 0, rp = n-1;
int imp = 0;
while (lp < rp && imp <= k) {
if (a[lp] + a[rp] == sum) {
++lp; --rp;
}else if (a[lp] + a[rp] < sum) {
++lp; ++imp;
}else{
--rp; ++imp;
}
}
if (imp > k) {
tried.insert(sum);
return 0;
}
get(sum);
return 1;
}
void get(int sum) {
int lp = 0, rp = n-1;
vector<int> ret;
while (lp < rp) {
if (a[lp] + a[rp] == sum) {
ret.pb(lp); ret.pb(rp);
++lp; --rp;
}else if (a[lp] + a[rp] < sum) {
++lp;
}else{
--rp;
}
}
while (SZ(ret) > n-k) {
ret.pop_back();
ret.pop_back();
}
sort(ALL(ret));
for (int t : ret) cout<<a[t]<<' ';
cout<<endl;
exit(0);
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
cin>>n>>k;
n+=k;
REP(i,n) cin>>a[i];
sort(a, a+n);
if (0 && n <= 200) {
// brute
REP(i,n) {
for (int to = i+1; to <n; ++to) {
// int to = n-i-1-j;
if (i < to && test(a[i] + a[to])) {
// get(a[i] + a[to]);
}
}
}
assert(0);
}
while (1) {
int i = rand() % n;
int j = rand() % (2*k+1);
int to = n-i-1-j+k;
bug(i,j,to);
if (i < to && to < n && test(a[i] + a[to])) {
}
}
}
# | 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... |