Skip to content

Commit 4365064

Browse files
Add files via upload
1 parent dc7ee8e commit 4365064

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
9+
10+
int an, bn;
11+
vector<ll> arr, brr;
12+
vector<ll> pfsa, pfsb; // pfsa[i] is the the sum of the best i pairs of arr=
13+
14+
15+
ll value(int fromA, int k){
16+
int fromB = k - fromA;
17+
return pfsa[fromA] + pfsb[fromB];
18+
}
19+
20+
int main(){
21+
fast_cin();
22+
int tc;
23+
cin >> tc;
24+
while (tc--){
25+
cin >> an >> bn;
26+
arr.resize(an);
27+
brr.resize(bn);
28+
29+
for (int i = 0; i < an; i++){
30+
cin >> arr[i];
31+
}
32+
for (int i = 0; i < bn; i++){
33+
cin >> brr[i];
34+
}
35+
36+
sort(arr.begin(), arr.end());
37+
sort(brr.begin(), brr.end());
38+
39+
pfsa.assign(an + 1, -1e9);
40+
pfsb.assign(bn + 1, -1e9);
41+
pfsa[0] = 0;
42+
pfsb[0] = 0;
43+
44+
for (int i=0,j=an-1; i<j; i++,j--){
45+
pfsa[i+1] = pfsa[i] + arr[j] - arr[i];
46+
}
47+
48+
for (int i=0,j=bn-1; i<j; i++,j--){
49+
pfsb[i+1] = pfsb[i] + brr[j] - brr[i];
50+
}
51+
52+
vector<ll> ans;
53+
for (int k=1;;k++){
54+
ll hi = min(k, an-k);
55+
ll lo = max(0, 2*k-bn);
56+
57+
if (hi < lo){
58+
break;
59+
}
60+
61+
// Ternary search for the best value of i, find maximum
62+
while (lo + 5 < hi){
63+
int m1 = lo + (hi - lo) / 3;
64+
int m2 = hi - (hi - lo) / 3;
65+
66+
if (value(m1, k) < value(m2, k)){
67+
lo = m1;
68+
} else {
69+
hi = m2;
70+
}
71+
}
72+
73+
ll best = -1e18;
74+
for (int i = lo; i <= hi; i++){
75+
best = max(best, value(i, k));
76+
}
77+
ans.push_back(best);
78+
79+
}
80+
cout << ans.size() << endl;
81+
for (auto x: ans){
82+
cout << x << " ";
83+
}
84+
cout << endl;
85+
}
86+
87+
return 0;
88+
}

0 commit comments

Comments
 (0)