Codeforces 304546 CSoldier and Cards(简单模拟题)

题意:
双方对战,每个牌都不同,从栈顶出牌,大的赢得对方牌,将其插入栈底,再自己牌插入栈底,问谁获胜,对战次数;双方无赢家打印**-1**。

思路:
模拟:deque
无输赢条件判断:最大循环次数为双方的全排列之和最大约为4e7。
0!*10!+1!*9!+2!*8!+。。。。+10!*0!约等于4e7。
4e7很慢,可以通过。
但1e6也行,也通过,技巧:一般出题人都会让循环控制在10的6次方内。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>
#define ll long long

using namespace std;
deque<int> q1,q2;
int main()
{
    ios::sync_with_stdio(false);
    int n,k1,k2,x;
    scanf("%d",&n);
    scanf("%d",&k1);
    for(int i=0;i<k1;i++)
    {
        scanf("%d",&x);
        q1.push_back(x);
    }
    scanf("%d",&k2);
    for(int i=0;i<k2;i++)
    {
        scanf("%d",&x);
        q2.push_back(x);
    }
    int i=0,flag=0,m=4000000;
    while(i<m)
    {
        if(q1.empty())
        {
            flag=2;
            break;
        }
        if(q2.empty())
        {
            flag=1;
            break;
        }
        int t1=q1.front(),t2=q2.front();
        q1.pop_front();
        q2.pop_front();
        if(t1>t2)
        {
            q1.push_back(t2);
            q1.push_back(t1);
        }
        else
        {
            q2.push_back(t1);
            q2.push_back(t2);
        }
        i++;
    }
    if(flag==0)
    {
        printf("-1\n");
    }
    else
    {
        printf("%d %d\n",i,flag);
    }
    return 0;
}

发布了23 篇原创文章 · 获赞 1 · 访问量 672

猜你喜欢

转载自blog.csdn.net/qq_43032263/article/details/104461387