Codeforces Round #642 (Div. 3) B. Two Arrays And Swaps

You are given two arrays aa and bb both consisting of nn positive (greater than zero) integers. You are also given an integer kk .

In one move, you can choose two indices ii and jj (1i,jn1≤i,j≤n ) and swap aiai and bjbj (i.e. aiai becomes bjbj and vice versa). Note that ii and jj can be equal or different (in particular, swap a2a2 with b2b2 or swap a3a3 and b9b9 both are acceptable moves).

Your task is to find the maximum possible sum you can obtain in the array aa if you can do no more than (i.e. at most) kk such moves (swaps).

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t2001≤t≤200 ) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and kk (1n30;0kn1≤n≤30;0≤k≤n ) — the number of elements in aa and bb and the maximum number of moves you can do. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ai301≤ai≤30 ), where aiai is the ii -th element of aa . The third line of the test case contains nn integers b1,b2,,bnb1,b2,…,bn (1bi301≤bi≤30 ), where bibi is the ii -th element of bb .

Output

For each test case, print the answer — the maximum possible sum you can obtain in the array aa if you can do no more than (i.e. at most) kk swaps.

Example
Input
Copy
5
2 1
1 2
3 4
5 5
5 5 6 6 5
1 2 5 4 3
5 3
1 2 3 4 5
10 9 10 10 9
4 0
2 2 4 3
2 4 2 3
4 4
1 2 2 1
4 4 5 4
Output
Copy
6
27
39
11
17
sort后分别取前k个,后k个进行比较,如果能使答案变优就交换。
#include <bits/stdc++.h>
using namespace std;
int a[35],b[35],n,k;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        int i,ans=0;
        for(i=1;i<=n;i++) scanf("%d",&a[i]);
        for(i=1;i<=n;i++) scanf("%d",&b[i]);
        sort(a+1,a+n+1);
        sort(b+1,b+n+1);
        for(i=1;i<=k;i++)
        {
            if(a[i]>b[n-i+1]) break;
            else swap(a[i],b[n-i+1]);
        }
        for(i=1;i<=n;i++)ans+=a[i];
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lipoicyclic/p/12894689.html