Codeforces546C:Soldier and Cards


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.

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值