Game of Credit Cards(田忌赛马)

传送门

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 1, 2, 3 and get no flicks at all, or he can name 2, 3 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

题解:

这算是一道思维题,听人说和田忌赛马有关。理解了之后挺简单的,之前想起来的,代码实现地太复杂了,当时就没做出来。

题意(意译):甲、乙各有一串数字,每个数字是独立的,如果一个人比另一个人的数字大,那么数低的人就要挨打一次,如果数字相同的话那么就平手。现在乙可以随意改变数字的顺序(其实和两个人都能随意改变顺序性质是一样的),要求乙挨打的最少次数和乙最多能打甲多少次。

用a[i],b[j]分别表示甲乙的数字。

思路:(1)要求甲挨打的最少次数,我们可以倒着求,先求乙赢以及甲乙平手的次数,然后用总数减去这个次数。

首先要使甲乙的数从小到大排序,如果乙要赢或者要平手,那么a[i]<=b[j],从小到大,以乙的尽量最小值去达到与甲平手或赢甲,这就可以实现输得次数最小化,注意循环最后要break一下,因为b[j]一个数参与比较并且满足条件后就不能用了。

(2)接下来求乙能打甲多少次,即乙最多能赢多少次。思路和上一步相似,以乙的最小值实现对甲的赢局。

#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn=1e3+10;

int a[1005],b[1005];
int vis[1005]; 

int main(){ 
//	freopen("input.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	
	int n;
	cin>>n;
	string s1,s2;
	cin>>s1>>s2;
	for(int i=0;i<n;i++) a[i]=s1[i]-'0',b[i]=s2[i]-'0';
	sort(a,a+n);
	sort(b,b+n);
	int sum1=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(a[i]<=b[j]&&vis[j]==0)
			{
				vis[j]=1;
				sum1++;
				break;
			}
		}
	}
	cout<<n-sum1<<endl;
	memset(vis,0,sizeof(vis));
	int sum2=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(b[i]>a[j]&&vis[j]==0)
			{
				sum2++;
				vis[j]=1;
				break;
			}
		}
	}
	cout<<sum2;

    return 0;
}

以前一直觉得想啊田忌赛马这样的问题自己写不出来,可是在比赛的时候自己真的差一点做出来了。写的题越多发现好多题,即使用到很有名的算法,自己原来也能想起来。

比赛是能发挥人的潜能的地方,连你都不知道你到底能做到什么地步!也许,你和别人差的只是最后的迎难而上!

对自己好一点,就算题再难也坚持积极认真思考到最后一刻,我绝不畏难,畏难的话就永远不能进步。

原创文章 99 获赞 15 访问量 7356

猜你喜欢

转载自blog.csdn.net/qq_45328552/article/details/101224423