题干:
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.
InputThe 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.
OutputFirst print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.
Examples3 123 321
0 2
2 88 00
2 0
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;
}