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.

思路:开两个数组map1,map2,下标代表数值,a[i]代表出现的次数,map1存a,map2存b,然后将b中最大的前k个数存入map1,然后输出map1前n个最大值

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int a[100] = { 0 };
        int b[100] = { 0 };
        int n, k;
        int c[40] = { 0 };
        int d[50] = { 0 };
        cin >> n >> k;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            c[a[i]]++;
        }
        for (int i = 0; i < n; i++)
        {
            cin >> b[i];
            d[b[i]]++;
        }
        for (int i = 30; i > 0; i--)
        {
            if (d[i] && k)
            {
                while (d[i] && k)
                {
                    c[i]++;
                    d[i]--;
                    k--;
                }
            }
            if (k <= 0) break;
        }
        int ans = 0;
        for (int i = 30; i > 0; i--)
        {
            if (c[i])
            {
                while (c[i] && n)
                {
                    ans +=i;
                    n--;
                    c[i]--;
                }
            }
            if (n <= 0) break;
        }
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yin101/p/12897134.html