Two Arrays And Swaps CodeForces - 1353B(贪心+分类)

题意:

给定两个数组,你可以交换 k 次 两个数组的元素,问最后 a 数组的和最大可以是多少?

题目:

You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also given an integer k.

In one move, you can choose two indices i and j (1≤i,j≤n) and swap ai and bj (i.e. ai becomes bj and vice versa). Note that i and j can be equal or different (in particular, swap a2 with b2 or swap a3 and b9 both are acceptable moves).

Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k such moves (swaps).

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.

Example

Input

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

Output

6
27
39
11
17

Note

In the first test case of the example, you can swap a1=1 and b2=4, so a=[4,2] and b=[3,1].

In the second test case of the example, you don’t need to swap anything.

In the third test case of the example, you can swap a1=1 and b1=10, a3=3 and b3=10 and a2=2 and b4=10, so a=[10,10,10,4,5] and b=[1,9,3,2,9].

In the fourth test case of the example, you cannot swap anything.

In the fifth test case of the example, you can swap arrays a and b, so a=[4,4,5,4] and b=[1,2,2,1].

分析:

将两个数组sort排序后,分别取前k个,后k个进行比较,如果能使答案变优即可以贪心,交换即可。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[35],b[35];
int t,n,k;
int main()
{scanf("%d",&t);while(t--){scanf("%d%d",&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;elseswap(a[i],b[n-i+1]);}for(i=1; i<=n; i++)ans+=a[i];printf("%d\n",ans);}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309744.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Abp v2.8.0发布 路线图

ABP框架和ABP商业版v2.8已经发布.这篇文章将涵盖这些发布中的新增内容和项目的中期路线图.ABP框架2.8有哪些新增内容?你可在GitHub的发行说明中看到所有的变更.这篇博客只包括重要的一些功能/变更.SignalR集成包我们已经发布了一个新的包用来集成SignalR到基于ABP框架应用程序…

贵州大学计算机专业的导师是谁,贵州大学计算机科学与信息学院导师介绍:王以松...

贵州大学计算机科学与信息学院导师介绍&#xff1a;王以松王以松&#xff0c;男&#xff0c;副教授&#xff0c;硕士研究生导师。主要研究方向&#xff1a;人工智能(知识表示与推理、逻辑程序设计)&#xff0c;语义网络等。 Em作者佚名次阅读2012-01-04王以松&#xff0c;男&am…

Board Moves CodeForces - 1353C(数学)

题意&#xff1a; 有一个 nn 的矩阵&#xff08;n 为奇数&#xff09;&#xff0c;每个矩阵单元有一个物品&#xff0c;每次操作你可将一个单元里的一个物品移动到该单元周围的八个单元里&#xff0c;问最后只有一个单元有物品的情况下&#xff0c;最少要多少次操作&#xff1…

BitArray虽好,但请不要滥用,又一次线上内存暴增排查

一&#xff1a;背景1. 讲故事前天写了一篇大内存排查在园子里挺火&#xff0c;这是做自媒体最开心的事拉&#xff0c;干脆再来一篇满足大家胃口&#xff0c;上个月我写了一篇博客提到过使用bitmap对原来的List<CustomerID>进行高强度压缩&#xff0c;将原来的List内存压缩…

2011年计算机基础知识试卷,2011年计算机一级考试理论试题:第六部分多选题

第6部分 信息与计算机基础知识 多选1.[ABC]通常来说&#xff0c;影响汉字输入速度的因素有 ______。(A)码长(B)重码率(C)是否有词组输入(D)有无提示行2.[BC]软件由______和______两部分组成。(A)数据(B)文档(C)程序(D)工具3.[BC]可以作为计算机存储容量的单位是______。(A)字母…

Constructing the Array CodeForces - 1353D(数据结构+分类+建设性算法)

题意&#xff1a; 有长度为 n 的数组 a &#xff0c;全为 0&#xff0c;接下来循环 n 次&#xff0c;每次选出一段最长的连续区间 [l, r]&#xff08;全为 0 &#xff0c;如果一样长&#xff0c;就选最左边的)。 如果 r−l1 是奇数&#xff0c;那么 a[lr2]ia[\frac{lr}{2}]ia…

[翻译]用于.NET Core的Windows窗体设计器发布

本文由微信公众号《开发者精选资讯》翻译首发&#xff0c;转载请注明来源今天我们很高兴地宣布&#xff0c;.NET Core 项目的 Windows 窗体设计器现在可以在 Visual Studio 2019 16.6 版中作为预览使用&#xff01;我们在 Visual Studio 16.7 预览版 1 中也提供了更新的设计器版…

运动会加油稿计算机学院,信息工程学院运动会加油稿

信息工程学院运动会加油稿1.你的汗水洒在跑道&#xff0c;浇灌着成功的花朵开放。你的欢笑飞扬在赛场&#xff0c;为班争光数你最棒。跑吧&#xff0c;追吧在这广阔的赛场上&#xff0c;你似骏马似离铉的箭。跑吧&#xff0c;追吧你比虎猛比豹强2你们挥舞着充满力量的双臂看着实…

K-periodic Garland CodeForces - 1353E(暴力+贪心+dp)

题意&#xff1a; 给定长为 n 的 0, 1 字符串&#xff0c;你可以通过一次操作改变一个字符&#xff08;0 变 1 or 1 变 0&#xff09;&#xff0c;问最少几次操作可以使任意相邻两个 1 之间的距离为 k ? 题目&#xff1a; You are given a garland consisting of n lamps. …

【视频回放与课件】零基础入门AI开发

今天上午&#xff0c;受广州图书馆邀请&#xff0c;在第一讲《零代码上手人工智能》的基础上&#xff0c;以《零基础入门AI开发》为主题&#xff0c;分四步解锁人工智能学习的概念与开发工具&#xff0c;让您在一小时内轻松掌握人工智能开发要领。本次课程内容主要包括&#xf…

三年级计算机群鸭戏水教案导入,三年级下册信息技术教案-3.7群鸭戏水-插入自选图形|清华版.doc...

第七课??《群鸭戏水——自选图形》??【教学内容分析】?本课&#xff0c;是小学信息技术(清华版)三年级下册第七课——自选图形的内容&#xff0c;通过前面章节的学习&#xff0c;学生已经学会了插入剪贴画和图片的操作。本节课创设了森林里要开动物运动会&#xff0c;请同…

Sequence with Digits CodeForces - 1355A(暴力+数学)

题意&#xff1a; 定义&#xff1a; an1anminDigit(an)maxDigit(an)。 给定 a1 和 k&#xff0c;求 ak &#xff1f; 题目&#xff1a; Let’s define the following recurrence: an1anminDigit(an)⋅maxDigit(an). Here minDigit(x) and maxDigit(x) are the minimal and …

Redis背后的故事

导语Redis已成为世界上最受欢迎的数据库之一&#xff0c;但当初正是因为Sanfilippo对数据库“缺乏经验”&#xff0c;使他敢于打破“良好”数据库工程的各种神圣规则&#xff0c;创建了Redis。正文如果Redis之父萨尔瓦多桑菲利波普&#xff08;Salvatore Sanfilippo&#xff09…

C++实现AOE网中的关键路径算法(邻接表存储)

代码如下: #include <iostream> #include <stack> #include <string> using namespace std; const int N 10010; using vnodeType int;typedef struct Node {int adj;int tw;//弧的时间权值Node *next; }Node;typedef struct Vnode {vnodeType v;//存储图…

哈工大威海计算机组成原理,哈工大威海计算机组成原理复习.pdf

第一章 绪论1.1 计算机的产生与发展现代计算机的发展电子管时代晶体管时代集成电路时代超大规模集成电路时代1.2 冯.诺伊曼计算机模型冯诺伊曼计算机的组成&#xff0c;各部分的作用.冯诺伊曼计算机的特点.(1) 计算机由运算器、存储器、控制器和输入设备、输出设备五大部件组成…