1. 程式人生 > 實用技巧 >ACM集訓STL(1)C題

ACM集訓STL(1)C題

C - Board Moves

You are given a board of size n×n, where n is odd (not divisible by 2). Initially, each cell of the board contains one figure.

In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell (i,j) you can move the figure to cells:

  • $(i−1,j−1)$;
  • $(i−1,j)$;
  • $(i−1,j+1)$;
  • $(i,j−1)$;
  • $(i,j+1)$;
  • $(i+1,j−1)$;
  • $(i+1,j)$;
  • $(i+1,j+1)$;

Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell.

Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. n2−1 cells should contain 0 figures and one cell should contain n2 figures).

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;
}