欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

codeforces B. Two Arrays And Swaps

程序员文章站 2022-06-05 13:09:16
...

codeforces B. Two Arrays And Swaps

题目

题意:

选择a,ba,b数据中的各kk个数字进行交换,最后要使得aa的总和最大。

思路:

我们将a,ba,b都排个序,然后在kk个数字中,如果出现bi>aib_i>a_i的情况,那么就交换。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
bool cmp(int a, int b) {return a > b;}
int main() {
    int t;
    read(t);
    while (t--) {
        int n, k;
        read(n), read(k);
        int a[50] = {0}, b[50] = {0};
        for (int i = 0; i < n; i++) read(a[i]);
        for (int i = 0; i < n; i++) read(b[i]);
        sort(a, a + n);
        sort(b, b + n, cmp);
        for (int i = 0; i < k; i++) {
            if (a[i] < b[i]) swap(a[i], b[i]);
        }
        int ans = 0;
        for (int i = 0; i < n; i++) ans += a[i];
        out(ans);
        putchar('\n');
    }
    return 0;
}

相关标签: codeforces