Codeforces Round #642 (Div. 3) B. Two Arrays And Swaps
程序员文章站
2022-03-12 17:09:28
...
原题:https://codeforces.com/contest/1353/problem/B
题意:t组案例,n为数组a,b大小,k为可交换次数,求经过k次交换后,数组a的和值的最大值
思路:sort排序,a升序,b降序,从头开始比较k次,a比b大就交换,在求和。
#include <bits/stdc++.h>
using namespace std;
int t,n,k,a[40],b[40],sum;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
cin>>t;
while(t--){
cin>>n>>k;
sum=0;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
sort(a,a+n);
sort(b,b+n,cmp);
for(int i=0;i<k;i++){
if(a[i]<b[i])
a[i]=b[i];}
for(int i=0;i<n;i++)
sum+=a[i];
cout<<sum<<endl;
}
}
上一篇: 快速排序-三路快排
下一篇: vc 怎样连接mysql数据库
推荐阅读
-
Codeforces Round #650 (Div. 3) B. Even Array
-
Codeforces Round #673 (Div. 2) B. Two Arrays(思维,构造)
-
codeforces B. Two Arrays And Swaps
-
Codeforces Round #479 (Div. 3) B. Two-gram
-
Codeforces Round #642 (Div. 3)
-
B. Two Arrays(模拟+思维)Codeforces Round #673 (Div. 2)
-
B. Polycarp Training(贪婪+排序)Codeforces Round #560 (Div. 3)
-
B. Disturbed People(模拟) Codeforces Round #521 (Div. 3)
-
E. Two Round Dances(圆排列)Codeforces Round #677 (Div. 3)
-
Codeforces Round #677 (Div. 3) E. Two Round Dances