cf#401(Div. 2)B. Game of Credit Card(田忌赛马类贪心)

题干:

After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.

Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.

Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 123 and get no flicks at all, or he can name 23 and 1 to give Sherlock two flicks.

Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.

The second line contains n digits — Sherlock's credit card number.

The third line contains n digits — Moriarty's credit card number.

Output

First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.

Examples
Input
3
123
321
Output
0
2
Input
2
88
00
Output
2
0
Note

First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.


题目大意:

    夏洛克和她玩起了弹脑壳的游戏。。。他们都拿到n个数字,并且夏洛克只能按照顺序念,她耍赖,可以挑着念(改变顺序)。然后,对于他们每一次念的数字,数字大的人可以给数字小的人一个脑瓜崩。问狡猾的她少可以被弹几次,&最多可以弹夏洛克几次?

解题报告:

    都是从小到大同序排序,然后挨个比较,类似田忌赛马,所以叫田忌赛马贪心。(从大到小亦可解?)


ac代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1000+5];
int b[1000+5];int main()
{int n;int ans1=0,ans2=0;cin>>n;for(int i = 0; i<n; i++) {scanf("%1d",&a[i]);}for(int i = 0; i<n; i++) {scanf("%1d",&b[i]);}
//	for(int i = 0; i<3; i++) {
//		printf("%d   %d\n",a[i],b[i]);
//	}sort(a,a+n);sort(b,b+n);int i=0,j=0;//a[i],b[j]对应 //解ans1 while(i<n&&j<n) {if(b[j]>=a[i]) {i++;j++;ans1++;}else if(b[j]<a[i]){j++;}}ans1=n-ans1;i=0;j=0;//解ans2 while( i < n && j < n) {if(b[j]>a[i]) {ans2++;i++;j++;}if(b[j]<=a[i]) {j++;}}printf("%d\n%d",ans1,ans2);return 0 ;} 
双端队列ac
#include<cstdio>
#include<queue>
#include<deque>
#include<algorithm>
using namespace std;
int main()
{char str1[1000+11],str2[1000+11];int a,b,n;while(~scanf("%d",&n)){deque<int> q1,q2;deque<int> s1,s2;scanf("%s %s",str1,str2);for(int i=0;i<n;i++){a=str1[i]-'0';q1.push_back(a);b=str2[i]-'0';q2.push_back(b);  }sort(q1.begin() ,q1.end() );sort(q2.begin() ,q2.end() );int k1,k2,v1,v2;int num1=n,num2=0;for(int i=1;i<=n;i++){k1=q1.back() ;k2=q2.back() ;if(k2>k1){num2++;s1.push_back(k1);s2.push_back(k2);q1.pop_back() ;q2.pop_back() ;continue;  }v1=q1.front() ;v2=q2.front() ;if(v2>v1){num2++;s1.push_back(v1);s2.push_back(v2);q1.pop_front() ;q2.pop_front() ;  }else{s1.push_back(k1);s2.push_back(v2);q1.pop_back() ;q2.pop_front() ;  }}sort(s1.begin() ,s1.end() );sort(s2.begin() ,s2.end() );for(int i=1;i<=n;i++){if(s2.back() >=s1.back() ){num1--;s1.pop_back() ;s2.pop_back() ;continue;}v1=s1.front() ;v2=s2.front() ;if(v2>=v1){num1--;s1.pop_front() ;s2.pop_front() ;}else{s1.pop_back() ;s2.pop_front() ;}}printf("%d\n%d\n",num1,num2);}return 0;} 



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

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

相关文章

NYOJ-14 会场安排问题(经典贪心,区间完全不覆盖模板)

附另一&#xff1a;此类问题选题总结&#xff1a;https://blog.csdn.net/qq_41289920/article/details/81001357 题干&#xff1a; 会场安排问题时间限制&#xff1a;3000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;4描述学校的小礼堂每天都会有许多活动&#xff0c;有…

可以使用田忌赛马类贪心问题的前提(或说 如何判断题目符合田忌赛马类贪心问题)

前提就是&#xff0c;首先&#xff0c;需要两个数组&#xff01;&#xff01;两个数组&#xff01;&#xff01;两个数组&#xff01;&#xff01;才可以考虑这个&#xff01; 其次&#xff0c;A和B这两个数组的个数是相同的。也正因为如此&#xff0c;所以从大到小排序或者从小…

【nyoj 270】数的分解(统计因子模板)

题干&#xff1a; 数的分解时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;1描述你的任务是找到一个最小的正整数Q&#xff0c;使Q的各位数的乘积等于N。输入最多450组测试数据。数据以EOF结尾。输入一个整数N&#xff08;0 ≤ N ≤ 400)。输出…

asp.net mvc 地址栏传输信息报错:window.location.pathname

asp.net mvc 地址栏传输信息报错&#xff1a;window.location.pathname 前端cshtml代码内容 代码片. // 页面跳转 window.location.pathname /Home/Index?cc123;“/”应用程序中的服务器错误 从客户端(?)中检测到有潜在危险的 Request.Path 值。 如何解决 web.config中添…

区间覆盖全部类型及部分精选习题汇总详解(贪心策略)

内容如下&#xff1a; 1&#xff09;区间完全覆盖问题 问题描述&#xff1a;给定一个长度为m的区间&#xff0c;再给出n条线段的起点和终点&#xff08;注意这里是闭区间&#xff09;&#xff0c;求最少使用多少条线段可以将整个区间完全覆盖 样例&#xff1a; 区间长度8&#…

数据库提示:正在还原中,无法访问 应该怎么办?

Sql语句 restore database 数据库名 with recovery

数据库工具一段时间后打开报错:远程过程调用失败0x800706be

1.原因可能是安装vs时自带更高版本的sql server服务造成的&#xff0c;只需要卸载Microsoft SQL Server 2012 Express LocalDB或 Microsoft SQL Server 2012 LocalDB就行了 2.先安装vs工具&#xff0c;然后安装数据库工具

【POJ - 1328】Radar Installation(贪心+计算几何)安装雷达辐射岛屿

题干&#xff1a;Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so …

sql语句实现分页查询

2005及以上版本 -- 分页查询&#xff08;通用型&#xff09; select top pageSize * from (select row_number() over(order by sno asc) as rownumber,* from student) temp_row where rownumber>((pageIndex-1)*pageSize);2012及以上版本 select * from student orde…

【51nod-1289】大鱼吃小鱼

题干&#xff1a; 有N条鱼每条鱼的位置及大小均不同&#xff0c;他们沿着X轴游动&#xff0c;有的向左&#xff0c;有的向右。游动的速度是一样的&#xff0c;两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向&#xff08;0表示向左&#xff0c;1表示向右&…

如何通过属性给实体赋值

获取实体属性 Type type family.GetType(); //family为实体对象 PropertyInfo[] infos type.GetProperties(); foreach (PropertyInfo info in infos){info.GetValue(family); //取值info.SetValue(bFamily, info.GetValue(family)); //赋值 }

【CF#801 A.】 Vicious Keyboard(字符串查找,水题)

题干&#xff1a;Tonio has a keyboard with only two letters, "V" and "K". One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one let…

关于ajax请求400问题解决

ajax请求&#xff1a;400 bad request 原因有两种&#xff1a; 参数不匹配&#xff0c;请求太长 如何解决 针对参数不匹配&#xff0c;只能一一对照 请求太长的话&#xff0c;api最好使用post方式请求&#xff0c;我遇到的问题就是post请求太长&#xff0c;这时候需要给参数…

【uva-673】 Parentheses Balance(括号匹配问题)

题干&#xff1a; You are given a string consisting of parentheses () and []. A string of this type is said to be correct:(a)if it is the empty string(b)if A and B are correct, AB is correct,(c)if A is correct, (A) and [A] is correct.Write a program that ta…

layui前端框架弹出框图标整理

1绿色对勾 2红色 3黄色问号 4黑色小锁 5红色哭脸 6绿色笑脸

【CF#757A】Gotta Catch Em' All!

题干&#xff1a;Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young…

layui 折叠面板使用无效问题

静态折叠面板无法使用 解决方案&#xff1a;layui.js 引用位置应该放在距离layui.use临近的地方 动态设置折叠面板无法点击折叠展开 解决方案&#xff1a;动态加载完界面后&#xff0c;执行下面语句&#xff1a; layui.element.init();

【HDU - 1031 】Design T-Shirt(水题 排序)

题干&#xff1a;Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA found that he was trapped by all kinds of suggestions from everyone on the board. It is indeed a mission-impossible to have everybody perfectly satisfie…

针对标签属性data-**的使用

在网站开发过程中&#xff0c;很多时候需要自定义一些标签属性&#xff0c;可以使用&#xff1a; 在html中&#xff1a; data-**"" 在js代码中取值&#xff1a; var **标签.data("**")

【HDU - 2203】 亲和串 (思维题,可选KMP)

题干&#xff1a;Problem Description人随着岁数的增长是越大越聪明还是越大越笨&#xff0c;这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考&#xff0c;因为他在很小的时候就知道亲和串如何判断了&#xff0c;但是发现&#xff0c;现在长大了却不知道怎么去…