【CodeForces - 546C】Soldier and Cards (vector或队列)

Soldier and Cards

老样子,直接上国语吧

 Descriptions:

两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌,放在自己手牌的最下方,而且对方输掉的那张手牌需要放在上面,自己赢的手牌放在下面。

Input

第一行的数n代表一共有几张牌

第二行第一个数x代表第一个人有x张牌

第三行第一个数y代表第二个人有y张牌

Output

第一个数代表进行了几轮,第二个数代表谁赢

Examples

Input

42 1 32 4 2

Output

6 2

Input

31 22 1 3

Output

-1

题目链接:

https://vjudge.net/problem/CodeForces-546C

我的做法可能有点偏了,因为不知道有几个牌,就用vector存一下,然后每次拿出双方的第一张牌进行比较,再进行模拟,现在想想可以用queue来写,当时写的有点粗糙,但是思路很清晰,一遍就AC了

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
vector<int>a;
vector<int>b;
int n;
int f,flag=1;
int x,y;
int sum=0;
int main()
{
    cin >> n;
    cin >> x;
    for(int i=0; i<x; i++)
    {
        int num;
        cin>>num;
        a.push_back(num);
    }
    cin>>y;
    for(int i=0; i<y; i++)
    {
        int num;
        cin>> num;
        b.push_back(num);
    }
    while(!a.empty()&&!b.empty())//两人的卡牌都不为空
    {
        sum++;
        int num1=a.front();//都取出第一张卡牌进行比较
        int num2=b.front();
        if(num1>num2)//1号大于2号就把2号和自己的卡牌一次放在队尾
        {
            a.push_back(num2);
            a.push_back(num1);
            vector<int>::iterator t1=a.begin();
            vector<int>::iterator t2=b.begin();
            a.erase(t1);
            b.erase(t2);
        }
        if(num2>num1)//2号大于1号就把1号和自己的卡牌一次放在队尾
        {
            b.push_back(num1);
            b.push_back(num2);
            vector<int>::iterator t1=a.begin();
            vector<int>::iterator t2=b.begin();
            a.erase(t1);
            b.erase(t2);
        }
        if(!b.empty())//谁的卡牌为空谁就输了
            f=2;
        if(!a.empty())
            f=1;
        if(sum>=n*n*n*n*n*n)//判断一下,要是循环这么多次还没分出胜负,应该就是死循环了
        {
            flag=0;
            cout<<"-1"<<endl;
            break;
        }
    }
    if(flag)
    cout<<sum<<" "<<f<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/sky-stars/p/10994998.html

时间: 2024-10-07 14:42:13

【CodeForces - 546C】Soldier and Cards (vector或队列)的相关文章

CodeForces 546C Soldier and Cards

题目链接:click here~~ [题目大意]两人玩牌,每次比较第一张牌的大小,放到底部,问最后谁赢,如果一直循环,则输出-1 [解题思路]设置两个队列,按照题目意思模拟一下即可,注意在循环一定次数下仍然没有结果,则无解 代码: <span style="font-family:SimSun;font-size:14px;">#include <bits/stdc++.h> using namespace std; const int N=1e6; queue

cf 546C Soldier and Cards

题目链接:C. Soldier and Cards Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different numb

546C. Soldier and Cards

题目链接 题意 两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张 给定输入的牌的顺序就是出牌的顺序 每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最下面,另外一张放在上面牌的上面,其他牌在放在这两张牌的上面. 求要pk多少次结束游戏,并记录赢得是哪个人 若出现死循环的情况输出 –1   这里可以根据栈或队列 java的程序是根据栈的,比较时候取出栈顶,加入新的两个 数的时候,要先出栈,在入栈,有点麻烦 Python程序是根据队列,在头取出进行比

队列 Soldier and Cards

Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have differ

CodeForces 546C(队列)

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between

queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

题目传送门 1 /* 2 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 3 queue容器:模拟上述过程,当次数达到最大值时判断为-1 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <stack> 11 #in

Codeforces Round #304 (Div. 2)——Cdfs——Soldier and Cards

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

Codeforces546C:Soldier and Cards

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

codeforces #304546 CSoldier and Cards (模拟)

Soldier and Cards Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different