Codeforces 546C Soldier and Cards【模拟】【期末考试全突击啊T T 】

本文介绍了一个简单的纸牌游戏,两名玩家轮流出牌比大小,胜者获得两张牌。通过模拟游戏过程,确定游戏结束所需的轮数及获胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Soldier and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to nall values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input

First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

All card values are different.

Output

If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.

If the game won't end and will continue forever output  - 1.

Examples
input
4
2 1 3
2 4 2
output
6 2
input
3
1 2
2 1 3
output
-1
Note

First sample:

Second sample:

题目大意:

两个人玩纸牌游戏,每一轮游戏每个人都把自己的第一个牌拿出来比较大小,大的一方先将对面的这个牌放在自己牌的最后,再把自己的牌放在最后,直到一个人没有了纸牌就算那个人输,问一共会进行多少次游戏,以及赢的人的编号。


思路(明天考Java敲完这题就要滚去预习啦好不开心):


观察到N最大也就是10,那么我们用两个队列来模拟这个游戏过程即可。

处理细节注意不要拿空队列中的元素。


T T期末考试突击鸭梨大啊,我空寂不住我积极啊...........我想敲题啊.............T T


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        queue<int >a;
        queue<int >b;
        int tmp;
        scanf("%d",&tmp);
        for(int i=0;i<tmp;i++)
        {
            int x;
            scanf("%d",&x);
            a.push(x);
        }
        scanf("%d",&tmp);
        for(int i=0;i<tmp;i++)
        {
            int x;
            scanf("%d",&x);
            b.push(x);
        }
        int cnt=0;
        int flag=0;
        while(cnt<=1400000)
        {
            int u=a.front();a.pop();
            int v=b.front();b.pop();
            cnt++;
            if(u>v)
            {
                a.push(v);
                a.push(u);
            }
            else
            {
                b.push(u);
                b.push(v);
            }
            if(a.size()==0&&b.size()==0)
            {
                break;
            }
            if(a.size()==0)
            {
                printf("%d %d\n",cnt,2);
                flag=1;
            }
            if(b.size()==0)
            {
                printf("%d %d\n",cnt,1);
                flag=1;
            }
            if(flag==1)break;
        }
        if(flag==0)printf("-1\n");
    }
}




### 关于重庆大学C++课程期末考试复习资料 对于准备重庆大学C++课程期末考试的学生而言,获取高质量的模拟试题和复习材料至关重要。通常情况下,学校官方会提供一些基础的学习资源,但为了更面地应对考试,建议采用多渠道收集复习资料的方法。 #### 官方推荐教材与教学大纲 首先应当依据学校的官方指定教材以及发布的最新版《计算机科学与技术》专业本科人才培养方案中的具体要求来制定复习计划[^1]。这些文件不仅明确了考核知识点范围,还提供了基本概念理解和编程技能训练的方向指导。 #### 过往试卷分析 通过向已毕业的同学询问或访问校园论坛等方式搜集历年来的期中期末测试题目是非常有效的途径之一。过往试卷往往能反映出教师命题思路的变化趋势及常考点分布情况,有助于考生把握重点难点并针对性练习。 #### 补充参考资料选择 除了上述提到的教学大纲外,还可以考虑引入其他辅助性的学习工具: - **书籍**:如《C++ Primer Plus》,该书深入浅出地讲解了C++语言特性及其实际应用场景;另外,《Effective Modern C++》也是一本不可多得的好书,书中总结了许多现代C++最佳实践案例。 - **在线平台**:LeetCode、Codeforces等网站上有大量针对不同难度级别的算法挑战项目可供刷题巩固理论知识的同时提高实战能力。 - **视频教程**:B站上的优质UP主分享了不少关于数据结构与算法设计方面的精彩内容,配合着做笔记模仿操作可以加深记忆理解程度。 ```cpp // 示例代码片段展示如何定义类成员函数实现简单的加法运算 class Calculator { public: int add(int a, int b); }; int Calculator::add(int a, int b){ return (a+b); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值