题解,太抽象了
最优情况一定可以是这样:
Code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200005]={0};
ll b[200005]={0};
ll l[200005]={0},r[200005]={0};
int main()
{
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--)
{
ll n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=m;i++)cin>>b[i];
sort(a+1,a+n+1);
sort(b+1,b+m+1);
ll left=n+1;
for(int i=1;i<=n;i++)
if(abs(a[i]-b[m-i+1])<abs(a[i]-b[n-i+1]))//这里千万不能小于等于!!!,因为此时的左占可能会把下一个的右占抢走
{
left=i;
break;
}
ll ans=0;
for(int i=1;i<=n;i++)
{
if(i<left)ans+=abs(a[i]-b[m-i+1]);
else ans+=abs(a[i]-b[n-i+1]);
}
cout<<ans<<endl;
}
return 0;
}
标签:Different,200005,Very,ll,cin,int,Array
From: https://www.cnblogs.com/pure4knowledge/p/17985998