Codeforces Round #642 (Div. 3) 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 (1≤i,j≤n1≤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.
The first line of the input contains one integer tt (1≤t≤2001≤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 (1≤n≤30;0≤k≤n1≤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 (1≤ai≤301≤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 (1≤bi≤301≤bi≤30 ), where bibi is the ii -th element of bb .
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.
5 2 1 1 2 3 4 5 5 5 5 6 6 5 1 2 5 4 3 5 3 1 2 3 4 5 10 9 10 10 9 4 0 2 2 4 3 2 4 2 3 4 4 1 2 2 1 4 4 5 4
6 27 39 11 17
sort后分别取前k个,后k个进行比较,如果能使答案变优就交换。
#include <bits/stdc++.h> using namespace std; int a[35],b[35],n,k; int main() { int t; cin>>t; while(t--) { cin>>n>>k; int i,ans=0; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) scanf("%d",&b[i]); sort(a+1,a+n+1); sort(b+1,b+n+1); for(i=1;i<=k;i++) { if(a[i]>b[n-i+1]) break; else swap(a[i],b[n-i+1]); } for(i=1;i<=n;i++)ans+=a[i]; cout<<ans<<endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!