ACM集训STL(1)B题

B - Two Arrays And Swaps

You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also given an integer k.

In one move, you can choose two indices i and j (1≤i,j≤n) and swap ai and bj (i.e. ai becomes bj and vice versa). Note that i and j can be equal or different (in particular, swap a2 with b2 or swap a3 and b9 both are acceptable moves).

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

You have to answer t independent test cases.

Input

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

The first line of the test case contains two integers n and k (1≤n≤30;0≤k≤n) — the number of elements in a and b and the maximum number of moves you can do. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤30), where ai is the i-th element of a. The third line of the test case contains n integers b1,b2,…,bn (1≤bi≤30), where bi is the i-th element of b.

Output

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

题解

#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int m,n,T;
    cin>>T;
    while(T--)
    {
        vector<int> v1,v2;
        int t;
        cin>>n>>m;
        for(int i=0;i<n;i++){
            cin>>t;
            v1.push_back(t);
        }
        for(int i=0;i<n;i++){
            cin>>t;
            v2.push_back(t);
        }
        sort(v1.begin(),v1.end());
        sort(v2.begin(),v2.end());
        while(m--)
        {
            if(*v1.begin()<*(v2.end()-1)){
                v1.erase(v1.begin());
                v1.push_back(*(v2.end()-1));
                v2.erase(v2.end()-1);
                sort(v1.begin(),v1.end());
            }
        }
        int sum=0;
        for(auto it:v1)sum+=it;
        cout<<sum<<endl;
        
    }
    return 0;
}
posted @ 2020-12-06 12:25  ambrumf  阅读(45)  评论(0编辑  收藏  举报