codeforces #304 546 C. Soldier and Cards() (模拟)

题目链接:

点击打开链接

题目大意:

两个人每个人有一个队列,每次取两人队头,然后比较,将两个放到比较大的那个人的队尾,然后判断胜负

题目分析:

直接模拟就好了

代码如下:

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

using namespace std;

int n,k1,k2,a;
queue<int> q1,q2;

int main ( )
{
    while ( ~scanf ( "%d" , &n ) )
    {
        scanf ( "%d" , &k1 );
        while ( k1-- )
        {
            scanf ( "%d" , &a );
            q1.push ( a );
        }
        scanf ( "%d" , &k2 );
        while ( k2-- )
        {
            scanf ( "%d" , &a );
            q2.push ( a );
        }
        int ans = 0;
        while ( true )
        {
            if ( q1.empty() || q2.empty() ) break;
            int u = q1.front() , v = q2.front();
            q1.pop();
            q2.pop();
            if ( u > v )
            {
                q1.push ( v );
                q1.push ( u );
            }
            else 
            {
                q2.push ( u );
                q2.push ( v );
            }
            ans++;
            if ( ans > 1e6 ) 
            {
                ans = -1;
                break;
            }
        }
        printf ( "%d" , ans );
        if ( ans != -1 )
            printf ( " %d\n" , q1.empty()?2:1);
        else puts("");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值