链接:https://ac.nowcoder.com/acm/contest/11166/G
来源:牛客网
题目描述
Given two arrays A, B with length N, you should perform exactly K operations in array A.
For each operation, choose 2 elements Ai,Aj(1≤i<j≤N)A_i, A_j(1 \leq i < j \leq N)Ai,Aj(1≤i<j≤N) and swap the positions of AiA_iAi and AjA_jAj.
Please maximize ∑i=1n∣Ai−Bi∣\sum_{i = 1}^{n}|A_i - B_i|∑i=1n∣Ai−Bi∣.
输入描述:
The first line of input contains two integers N,K(2≤N≤5×105,0≤K≤108)N, K(2 \leq N \leq 5 \times 10^5, 0 \leq K \leq 10^8)N,K(2≤N≤5×105,0≤K≤108), describing the length of A, B and number of operations. The second line contains N integers A1,...,AN(−108≤Ai≤108)A_1,...,A_N(-10^8 \leq A_i \leq 10^8)A1,...,AN(−108≤Ai≤108). The third line contains N integers B1,...,BN(−108≤Bi≤108)B_1,...,B_N(-10^8 \leq B_i \leq 10^8)B1,...,BN(−108≤Bi≤108).
输出描述:
Output one integer, the maximum answer.
示例1
输入
3 2 1 2 3 3 2 1
输出
4
示例2
输入
3 2 1 2 3 1 2 3
输出
4
示例3
输入
3 1 1 2 3 3 2 1
输出
4
n为a,b中的元素个数
m为可操作次数
先把a中大于b中的数全换成b中的数
然后a,b分别排序
a从后往前
b从前往后
a>b就相减乘2
然后总和相加即可
#include<iostream>
#include<algorithm>
using namespace std;
long long a[1000005],b[1000005];
int main()
{
long long n,m;
cin>>n>>m;
for(long long i=0;i<n;i++)cin>>a[i];
for(long long i=0;i<n;i++)cin>>b[i];
long long sum=0;
for(long long i=0;i<n;i++)
{
if(a[i]>b[i])
{
swap(a[i],b[i]);
}
sum+=abs(a[i]-b[i]);
}
sort(a,a+n);
sort(b,b+n);
for(int i=0;i<m&&i<n;i++)
{
if(a[n-i-1]>b[i])
sum+=2*(a[n-i-1]-b[i]);
else
break;
}
cout<<sum<<endl;
return 0;
}