codeforces B. Two Arrays And Swaps
程序员文章站
2022-06-05 13:09:16
...
题目
题意:
选择数据中的各个数字进行交换,最后要使得的总和最大。
思路:
我们将都排个序,然后在个数字中,如果出现的情况,那么就交换。
#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 Round #673 (Div. 2) B. Two Arrays(思维,构造)
-
codeforces B. Two Arrays And Swaps
-
Codeforces Round #479 (Div. 3) B. Two-gram
-
B. Two Arrays(模拟+思维)Codeforces Round #673 (Div. 2)
-
E. Two Arrays and Sum of Functions(数学问题) Codeforces Round #560 (Div. 3)
-
CodeForces - 1288C Two Arrays(组合数学)
-
codeforces 1260 B. Obtain Two Zeroes
-
Codeforces Round #268 (Div. 1)-B. Two Sets_html/css_WEB-ITnose
-
Codeforces Round #268 (Div. 1)-B. Two Sets_html/css_WEB-ITnose
-
Codeforces Round #560 (Div. 3) -> E. Two Arrays and Sum of Functions