【CodeForces - 1038C】Gambling (博弈问题,优先队列模拟,贪心)

题干:

Two players A and B have a list of nn integers each. They both want to maximize the subtraction between their score and their opponent's score.

In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent's list (assuming his opponent's list is not empty).

Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements {1,2,2,3}{1,2,2,3} in a list and you decided to choose 22 for the next turn, only a single instance of 22will be deleted (and added to the score, if necessary).

The player A starts the game and the game stops when both lists are empty. Find the difference between A's score and B's score at the end of the game, if both of the players are playing optimally.

Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent's score, knowing that the opponent is doing the same.

Input

The first line of input contains an integer nn (1≤n≤1000001≤n≤100000) — the sizes of the list.

The second line contains nn integers aiai (1≤ai≤1061≤ai≤106), describing the list of the player A, who starts the game.

The third line contains nn integers bibi (1≤bi≤1061≤bi≤106), describing the list of the player B.

Output

Output the difference between A's score and B's score (A−BA−B) if both of them are playing optimally.

Examples

Input

2
1 4
5 1

Output

0

Input

3
100 100 100
100 100 100

Output

0

Input

2
2 1
5 6

Output

-3

Note

In the first example, the game could have gone as follows:

  • A removes 55 from B's list.
  • B removes 44 from A's list.
  • A takes his 11.
  • B takes his 11.

Hence, A's score is 11, B's score is 11 and difference is 00.

There is also another optimal way of playing:

  • A removes 55 from B's list.
  • B removes 44 from A's list.
  • A removes 11 from B's list.
  • B removes 11 from A's list.

The difference in the scores is still 00.

In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be 00.

题目大意:

有A,B两个竞赛者各自拥有一个序列,他们可以在每一个回合中可以任意进行两种操作的其中一种,第一种操作是在对方的序列中删除一个元素,第二种操作是把自己序列中的一个元素加入自己的得分中,A,B都想要自己的得分较高,输出A-B的值。

解题报告:

    这题贪心,用优先队列模拟一下,也可以直接数组模拟一下,不难做,有一个类似的题目需要用dp去求解【UVA - 10891 Game of Sum 】dp,博弈

    对于这题注意一下a为空或者b为空的情况就可以了。

   另外。代码格式方面,建议while(1)然后if(..)break;elseif();elseif();else这样的格式,这样层次分明,并且不会拉下a为空或者b为空的情况,一开始写代码的时候就落下了这两种情况。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;ll tmp,suma,sumb;
priority_queue<ll> a,b;
int main()
{int n;bool flag = 1;//1代表先手a,2代表后手b cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",&tmp),a.push(tmp);for(int i = 1; i<=n; i++) scanf("%lld",&tmp),b.push(tmp);while(a.size() || b.size()) {if(a.empty()) {if(!flag) sumb += b.top();b.pop();flag = !flag;continue;}if(b.empty()) {if(flag) suma += a.top();a.pop();flag = !flag;continue; }if(flag) {if(a.top() >= b.top()) {suma += a.top();a.pop();} else {b.pop();}}else {if(b.top() >= a.top()) {sumb += b.top();b.pop();}else {a.pop();}}flag = !flag;} printf("%lld\n",suma - sumb);return 0 ;
}//18:04 - 18:17

 

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

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

相关文章

乐乐勇智能教育机器人有多少型号_【头条】协作机器人平台化趋势将会是柔性自动化的破局之道...

智能机器人商情微信公众号&#xff0c;关注中国智能机器人行业热点与发展趋势&#xff0c;打造快捷高效的行业资讯交互平台。更多精彩内容&#xff0c;您可以点击标题下方的蓝字关注我们。导语艾利特平台级CS系列协作机器人全新发布9月15日上海工博会第一天&#xff0c;艾利特机…

计算机毕设结束语致谢,毕业设计结束语和致谢

毕业设计结束语和致谢时间&#xff1a;2021-01-17 20:08:40 分类&#xff1a;小结范文 | 毕业论文致谢词范文 | Word文档下载毕业设计结束语和致谢导语&#xff1a;论文致谢应以简短的文字对课题研究与论文撰写过程中间直接给予帮助的人员(例如指导教师、答疑教师及其他人员)…

*【HDU - 2819】Swap(二分图匹配,输出路径)(待证明:是否是最少交换次数?)

题干&#xff1a; Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. Can you find a way to make all the diagonal entries equal to 1? Input There are several test cases in the input. The first line of each …

CSS中属性个属性值怎么区分,[CSS] 详细解释 @media 属性与 (max

前言现在 HTML5/CSS3 很流行罢&#xff0c;也是未来时代的趋势。在 HTML5 带来的许多实用功能之后&#xff0c;CSS3也同带来了一些牛逼哄哄的功能呢。动画 animation转化 transform过渡 translation尽快这已足够让我们兴奋&#xff0c;许多之前必须用 JS 或 JQ 写的效果用 CSS …

西门子精智和精简面板区别_西门子S7-1200的功能与特点,应用范围介绍

S7-1200是西门子公司新推出的一款面向离散自动化系统和独立自动化系统的低端PLC。S7-1200采用了模块化设计&#xff0c;具备强大的工艺功能&#xff0c;适用于多种场合&#xff0c;可以满足不同的自动化需求。S7-1200的定位处于原有的SIMATIC S7-200和SIMATIC S7-300之间&#…

【HDU - 1281 】棋盘游戏 (经典的二分图匹配,匈牙利算法,枚举删除顶点,必须边,关建边)

题干&#xff1a; 小希和Gardon在玩一个游戏&#xff1a;对一个N*M的棋盘&#xff0c;在格子里放尽量多的一些国际象棋里面的“车”&#xff0c;并且使得他们不能互相攻击&#xff0c;这当然很简单&#xff0c;但是Gardon限制了只有某些格子才可以放&#xff0c;小希还是很轻松…

eclipse提示方法已过时_提高效率,eclipse上你可能不知道的技巧

一张思维导图1、控制台(console )日志输出另保存经常会遇到这种情况&#xff0c;习惯性的清掉控制台上的输出日志&#xff0c;再然后发现刚才的日志居然还有用&#xff0c;不得不又重新调试一遍&#xff0c;为了解决这种“手贱”的问题&#xff0c;我在网上搜了一些资料&#x…

【CodeForces - 144B 】Meeting (暴力枚举,水题,计算几何)

题干&#xff1a; The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super Duper Secret Place. The place is an infinite plane with introduced Cartesian coordinate system. The meeting table is represented as a rectangle…

html中超链接使用_干货 | HTML中表格的使用方法

HTML中的表格是由表格的标签组成HTML中的表格是由标签用于定义表格的列&#xff0c;标签为又是标签来分割列&#xff0c;形成一个完整的表格。表格的标签组合关系为&#xff1a;我是单元格1我是单元格2表格中可以插入文本、图片、列表、段落、表单、水平线等任何html标签&#…

chrome插件中调用ajax,Chrome扩展程序中的Ajax调用无效

尝试在Chrome扩展程序中发送ajax请求。我已经确认请求返回200响应&#xff0c;它应该只是console.log来测试&#xff06;&#xff03;39;。我不确定这里是否存在异常问题&#xff1f;我已经阅读了Chrome扩展程序&#xff06;&#xff03;39; addListener&#xff06;&#xff0…

【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)

题干&#xff1a; A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string "aab" is an anagram of the string "aba" and the string "…

互补品的交叉弹性系数_重庆事业单位综合备考:需求价格弹性和需求交叉价格弹性有何区别...

需求价格弹性 VS 需求交叉价格弹性一、需求价格弹性指某种商品随着价格的变化&#xff0c;该商品需求变化的程度大小(这里指的是同一种商品)。或者说&#xff0c;是需求变化相对于价格变化的敏感程度。假设&#xff1a;黄金、IPONE手机等商品降价50%&#xff0c;一定会大卖&…

esxi服务器与虚拟机时间不符,vsphere6.7-虚拟机与ESXI时间同步

ELF Format 笔记(十五)—— 符号哈希表ilocker:关注 Android 安全(新手) QQ: 2597294287 符号哈希表用于支援符号表的访问,能够提高符号搜索速度. 下表用于解释该哈希表的组织,但该格式并不属于 ELF 规范. ...AD域修改组策略如果我们的计算机加入AD域之后,修改安全策略时不能用…

朋友圈自动回复评论_微信新版,朋友圈可以表情包回复了!网友:评论区斗起来.jpg...

你们发现了吗&#xff1f;朋友圈可以发表情包评论了&#xff01;微信iOS版7.0.9正式版今天迎来更新支持发消息时可以引用之前的内容更令人惊喜的是不少网友都发现新版本还新增朋友圈自定义表情评论功能可以用表情包评论别人的朋友圈内容了&#xff01;轻触表情包还可以查看大图…

【CodeForces - 144D】Missile Silos(单源最短路,枚举中间边,枚举情况可能性)

题干&#xff1a; A country called Berland consists of n cities, numbered with integer numbers from 1to n. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. Accordi…

第九大陆服务器未找到文件,第九大陆服务器优化规则说明 拍卖行和寄售功能关闭公告...

第九大陆已经确定了六月份服务器的优化时间与规则&#xff0c;同时为了配合6月8日的服务器优化实施&#xff0c;拍卖行将于6月5日关闭&#xff0c;待服务器优化完成后即可开放&#xff01;给大家带来的不便&#xff0c;敬请您谅解&#xff01;《第九大陆》服务器优化详细规则说…

【牛客 - 210A】游戏(思维,脑洞)

题干&#xff1a; BLUESKY007,fengxunling和dreagonm三个人发现了一个像素游戏,这款神奇的游戏每次会生成一个nxm的网格,其中每一个格子都被随机染色为R,G,B三种颜色之一,每次都可以选择任意一个非B颜色的格子进行一次操作,每次操作都会满足以下规则&#xff1a; 1.操作的范围为…

建房子 最安全图纸_妄想山海初期该怎么办?砍树狩猎建房子,还能拆别人的房子...

妄想山海还是一个很有趣的游戏&#xff0c;唯一的缺点就是游戏对手机配置要求比较高&#xff0c;手机太差的玩家&#xff0c;就别想那么多了&#xff0c;希望等游戏正式上线&#xff0c;能优化的好一些吧&#xff01;在测试中&#xff0c;玩家能体验到游戏中的各种玩法&#xf…

【POJ - 2226】Muddy Fields(匈牙利算法 或 网络流dinic,二分图匹配,最小点覆盖,矩阵中优秀的建图方式 )

题干&#xff1a; Rain has pummeled the cows field, a rectangular grid of R rows and C columns (1 < R < 50, 1 < C < 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, dont w…

springboot的原生cache_springboot-shiro-redis-session-cache

1.配置mysql和redis redis的需要配置密码 原生jedis的使用相对于spring-data-redis更加灵活 所以本项目用原生jedis来直接作为缓存使用 具体使用方法请参看相关配置 特别注意如果mysql是5.7以上版本&#xff0c;请去掉sql_mode中的ONLY_FULL_GROUP_BY 本项目用户支持多角色多部…