【CF】--- Game of Credit Cards (双端队列&&贪心)

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.

Example
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张牌,每一张牌的数字大小都不同,如果谁出的牌较小就会挨打。A是个老实人,他会按照所给顺序出牌,但是B就不一样了,他太不老实了,为了不挨打,会偷偷地改变出牌顺序,也就是说,B会根据A的出牌方式合理地选择自己的出牌策略。
看着就是一个“田忌赛马”呀,可不是博弈论(脑子里总想着博弈,给自己暗示)。
用双端队列。
问B被挨打的最少次数,和A挨打的最多次数。两个结果可以采取不同的策略,也就是说这是两个不同的过程。
先研究A挨打最多的次数:
1)先让A和B最大的数字进行比较,如果B的较大,则B赢,A挨打一次,依次比较当前最大的;
2)如果1)不成立,那就比较A和B最小的,如果B的较大,则B赢,A挨打一次,下面再循环比较最大的。
3)如果上述两个都不成立,B最大的和最小的都比A的小或者相等,那就让B最小的和A最大的去比,反正也是要么输要么平局,不如输得悲壮一些,拉A最大的下水。A并没有挨打哦。
再研究B挨打最小的次数(B挨打次数初始化为n):
1)先让A和B最大的数字进行比较,如果B的较大或相等,那么B不用挨打,挨打次数减1,下面循环比较最大的。
2)如果1)不成立,那就比较A和B最小的,如果B的较大或相等,那么B不用挨打,挨打次数减1,再循环比较最大的。
3)如果上述两个都不成立,B最大的和最小的都比A的小或者相等,那就让B最小的和A最大的去比,反正也是要么输要么平局,不如输得悲壮一些,拉A最大的下水。那B就挨打了,挨打次数不发生变化。

上述用到了两次从小到大排序的双端队列,第一次比较出队时可直接进入第二次相对应的队列中,然后排好序,再进行比较。

代码如下:

#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;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值