-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF2063D.cpp
115 lines (110 loc) · 1.98 KB
/
CF2063D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define N 200010
int n,m;
int a[N],b[N];
long long pa[N],pb[N];
long long cal(int k, int x)
{
// cout<<"cal"<<k<<' '<<x<<' '<<pa[x]+pb[k-x]<<endl;
return pa[x]+pb[k-x];
}
long long gao(int k)
{
long long ans = 0;
int l = max(0,2*k-m);
int r = min(k,n-k);
// cout<<"gaob"<<k<<' '<<l<<' '<<r<<' '<<n<<' '<<k<<endl;
while(l<=r)
{
int l1 = l+(r-l)/3;
int l2 = l+(r-l)/3*2;
if (l1 == l2)
{
ans = max({cal(k,l),cal(k,r)});
if (l<r)
{
ans = max(ans, cal(k,l+1));
}
break;
}
long long a1 = cal(k,l1);
long long a2 = cal(k,l2);
ans = max(a1,a2);
if (a1<a2)
{
l = l1;
}
else
{
r = l2;
}
}
// cout<<"gao"<<k<<' '<<ans<<endl;
return ans;
}
void solve()
{
int kmax=min({n,m,(n+m)/3});
cout<<kmax<<endl;
sort(a,a+n);
sort(b,b+m);
pa[0]=0;
// cout<<"pa";
for(int i=0;i<n/2;i++)
{
if (n-i-1 == i)
{
break;
}
pa[i+1] = a[n-i-1]-a[i];
pa[i+1]+=pa[i];
// cout<<pa[i+1]<<' ';
}
// cout<<endl;
// cout<<"pb";
pb[0]=0;
for(int i=0;i<m/2;i++)
{
if (m-i-1 == i)
{
break;
}
pb[i+1] = b[m-i-1]-b[i];
pb[i+1]+=pb[i];
// cout<<pb[i+1]<<' ';
}
// cout<<endl;
for(int i=1;i<=kmax;i++)
{
if (i>1)
{
cout<<' ';
}
cout<<gao(i);
}
cout<<endl;
}
int main()
{
#ifdef CODEBLOCKS
freopen("in.txt", "r", stdin);
#endif // CODEBLOCKS
int t;
cin>>t;
for(int ti=0;ti<t;ti++)
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<m;i++)
{
cin>>b[i];
}
solve();
}
return 0;
}