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 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.

Sample test(s)

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:

题意:
有两个人,每个人又一些牌,总张数不超过10,每张牌都有个不同价值,每个人的牌的标号固定的,出牌只能选标号最小的出,然后比较大小,大的那个获胜,得到对方的牌,并把获得的这张牌与自己出的那张牌,都放到尾部,然后如此循环,最后没有手牌的人输掉比赛

思路:
直接标记状态搜索

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 5000005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;

map<string,map<string,int> > vis;
int n;
int k1,k2;
int a[15],b[15],c[15];
char s1[15],s2[15];

int main()
{
    int i,j,k;
    scanf("%d",&n);
    scanf("%d",&k1);
    for(i = 0; i<k1; i++)
    {
        scanf("%d",&a[i]);
        c[i] = a[i];
    }
    scanf("%d",&k2);
    for(i = 0; i<k2; i++)
    {
        scanf("%d",&b[i]);
        c[k1+i] = b[i];
    }
    sort(c,c+k1+k2);
    for(i = 0; i<k1; i++)
    {
        for(j = 0; j<k1+k2; j++)
        {
            if(a[i]==c[j])
                s1[i] = j+'0';
        }
    }
    s1[k1] = '\0';
    for(i = 0; i<k2; i++)
    {
        for(j = 0; j<k1+k2; j++)
        {
            if(b[i]==c[j])
                s2[i] = j+'0';
        }
    }
    s2[k2] = '\0';
    vis[s1][s2] = 1;
    int ans = 0;
    while(k1&&k2)
    {
        int p1 = s1[0],p2 = s2[0];
        if(p1>p2)
        {
            for(i = 0; i<k2; i++)
                s2[i] = s2[i+1];
            k2--;
            for(i = 0; i<k1; i++)
                s1[i] = s1[i+1];
            s1[k1-1] = p2;
            s1[k1] = p1;
            k1++;
            s2[k2] = s1[k1] = '\0';
        }
        else
        {
            for(i = 0; i<k1; i++)
                s1[i] = s1[i+1];
            k1--;
            for(i = 0; i<k2; i++)
                s2[i] = s2[i+1];
            s2[k2-1] = p1;
            s2[k2] = p2;
            k2++;
            s2[k2] = s1[k1] = '\0';
        }
        if(vis[s1][s2])
        {
            ans = -1;
            break;
        }
        ans++;
        vis[s1][s2] = 1;
    }
    printf("%d",ans);
    if(ans!=-1)
    {
        if(k1)
            printf(" 1");
        else
            printf(" 2");
    }
    printf("\n");

    return 0;
}
时间: 2024-11-09 20:16:44

Codeforces546C:Soldier and Cards的相关文章

Codeforces546C: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 number of card

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

Soldier and Cards 老样子,直接上国语吧  Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌,放在自己手牌的最下方,而且对方输掉的那张手牌需要放在上面,自己赢的手牌放在下面. Input 第一行的数n代表一共有几张牌 第二行第一个数x代表第一个人有x张牌 第三行第一个数y代表第二个人有y张牌 Output 第一个数代表进行了几轮,第二个数代表谁赢 Examples Input 42 1 32

队列 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

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

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

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

546C. Soldier and Cards

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

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

题解 CF546C 【Soldier and Cards】

思路 是一道水题,可以用队列+模拟来写,注意不要拿完队列中的元素! 代码 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int maxn=1000000+5; 6 int main() 7 { 8 int n; 9 cin>>n; 10 queue<int>a;//队列 11 queue<int>b; 1