您的位置:首页 > 其它

【CodeForces 777B】Game of Credit Cards(贪心+排序)

2017-02-25 09:51 411 查看


Game of Credit Cards

Problem Description

Afterthe fourth season Sherlock and Moriary have realized the whole foolishness ofthe battle between them and decided
to continue their competitions in peacefulgame of Credit Cards.

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

Of course, Sherlock will play honestly naming digits one by onein 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 tochange the overall number of occurences of each digit). For example, in caseabove Moriarty could name1,2,3and
get no flicks at all,or he can name2,3and1to
give Sherlock twoflicks.

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

Input

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

The second line containsndigits
— Sherlock's creditcard number.

The third line containsndigits
— Moriarty's creditcard number.

Output

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



#include<iostream>
#include<cstdio>
#include<algorithm>
#define manx 1005
using namespace std;
int main()
{
int n,a[manx]={0},b[manx]={0};
char c;
scanf("%d",&n);
getchar();
for (int i=0; i<n; i++){  //注意输入
scanf("%c",&c);
a[i]=c-'0';
}
getchar();
for (int i=0; i<n; i++){
scanf("%c",&c);
b[i]=c-'0';
}
sort(a,a+n);
sort(b,b+n);
int i,j,ans1=0,ans2=0;
for (i=0,j=0; i<n && j<n; ){
if(b[j] >= a[i]){
i++;
j++;
}
else {
ans1++;
j++;
}
}
for (i=n-1,j=n-1; i>=0 && j>=0; ){  //田忌赛马
if(b[j] > a[i]){
ans2++;
i--;
j--;
}
else i--;
}
printf("%d\n%d\n",ans1,ans2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: