#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int n, k;
if (!(cin >> n >> k)) return;
int m = n + k;
vector<ll> a(m);
for (int i = 0; i < m; i++) cin >> a[i];
// İki işaretçi için diziyi küçükten büyüğe sıralıyoruz
sort(a.begin(), a.end());
// Aday toplamları (S) belirliyoruz.
// Toplam K eleman dışarıda kalacağı için S mutlaka
// ilk K+1 ve son K+1 eleman kombinasyonlarından biridir.
vector<ll> candidates;
for (int i = 0; i <= k; i++) {
for (int j = m - 1 - (k - i); j < m; j++) {
candidates.push_back(a[i] + a[j]);
}
}
// Adayları teke indirip sıralıyoruz ki gereksiz kontroller yapmayalım
sort(candidates.begin(), candidates.end());
candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());
// Two Pointers ile her adayı test et
for (ll S : candidates) {
int L = 0, R = m - 1;
int count = 0;
// n/2 tane çift bulmamız gerekiyor (toplam n eleman)
while (L < R) {
ll current_sum = a[L] + a[R];
if (current_sum == S) {
count++;
L++;
R--;
} else if (current_sum < S) {
L++;
} else {
R--;
}
if (count * 2 == n) break;
}
// Eğer n elemanı (n/2 çift) tamamladıysak sonucu yazdır
if (count * 2 == n) {
// Tekrar Two Pointers ile elemanları yazdırıyoruz
L = 0; R = m - 1;
int printed = 0;
while (L < R && printed < n) {
if (a[L] + a[R] == S) {
cout << a[L] << " ";
// Sağdaki elemanı sonra yazdırmak için saklayabiliriz
// ama problem sırayla yazdırmayı genelde kabul eder.
// En güvenlisi bulduğumuz elemanları bir vector'e atıp yazmak:
}
// (Yukarıdaki mantığı temizlemek için aşağıda vector kullanıyoruz)
// ...
}
// Daha temiz çıktı için tekrar çalıştıralım:
vector<ll> res;
int l2 = 0, r2 = m - 1;
while(l2 < r2 && res.size() < n) {
if(a[l2] + a[r2] == S) {
res.push_back(a[l2]);
res.push_back(a[r2]);
l2++; r2--;
} else if(a[l2] + a[r2] < S) l2++;
else r2--;
}
sort(res.begin(), res.end());
for(int i = 0; i < n; i++) cout << res[i] << (i == n-1 ? "" : " ");
cout << endl;
return;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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... |