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

Input

4
2 1 3
2 4 2

Output

6 2

Input

3
1 2
2 1 3

Output

-1

Sample Output

Hint

First sample:

Second sample:

题意:

游戏双方各有一些牌,牌上都有一个各不相同的值,每一轮取出最上面的牌进行比较,大的一方把对方的牌压在自己的一沓牌的下面,然后再把自己的这张牌再次压在最下面,就这样一直进行下去,直到有一方没有牌或者不会出现没有牌的情况为止。如果一直循环,输出-1,如果没有循环,输出游戏进行的次数和哪一方获胜。

思路:

定义两个队列,分别取出两个队列的队首,进行比较,如果q1的队首大于q2的队首,把q2的队首放在在q1队尾,然后再把q1的队首放在q1的队尾。如果q2的队首大于q1的队首,把q1的队首放在q2的队尾,然后把q2的队首放在q2的队尾。

代码:

<pre class="cpp" name="code">#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    queue<int>q1,q2;
    int n,k1,k2,a;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%d",&k1);
    for(int i=0;i<k1;i++)
     {
          scanf("%d",&a);
          q1.push(a);

     }
     scanf("%d",&k2);
     for(int i=0;i<k2;i++)
     {
          scanf("%d",&a);
          q2.push(a);

     }
     int ans=0;
     while(1)
     {
         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);
    }
    return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-09 00:34:57

codeforces #304546 CSoldier and Cards (模拟)的相关文章

Codeforces 475C Kamal-ol-molk&#39;s Painting 模拟

题目链接:点击打开链接 题意:给定n*m的矩阵 X代表有色 .代表无色 用一个x*y的矩阵形刷子去涂色. 刷子每次可以→或↓移动任意步. 若能够染出给定的矩阵,则输出最小的刷子的面积 若不能输出-1 思路: 先找到连续最小的x,y 因为至少一个边界和x或y相等,所以枚举(x,i) 和 (i,y)就可以了. #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <

Codeforces 30D King&#39;s Problem? 模拟

首先将n个点排序,找出排序后的K,然后分情况讨论. 当 k == n+1时,显然是 k->1->n || k->n->1这两种的较小值,因为三角形的两边之和大于第三边. 当1 <= k && k <= n 时: 1 , k -> 1 -> n+1 -> k+1 ->n  ||  k -> n -> n+1 -> k-1 -> 1,当k+1 || k-1 不存在时将对应步骤忽略. 2 , k - > 1

Codeforces 747C:Servers(模拟)

http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开始,如果可以完成任务,那么输出id总和,否则输出-1. 思路:简单的模拟,注意如果不能完成任务,那么ser数组是不能更新的. 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #includ

CodeForces 670 A. Holidays(模拟)

Description On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Martians have the same weeks as earthlings — 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible nu

Codeforces 583 DIV2 Asphalting Roads 模拟

原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #include<iostream> #include<cstring> #include<algorithm> #define MAX_N 55 using namespace std; bool h[MAX_N],v[MAX_N]; int n; int main(){ cin

CodeForces - 670C Cinema (map&amp;模拟)水

CodeForces - 670C Cinema Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of th

CodeForces - 586C Gennady the Dentist 模拟(数学建模的感觉)

http://codeforces.com/problemset/problem/586/C 题意:1~n个孩子排成一排看病.有这么一个模型:孩子听到前面的哭声自信心就会减弱:第i个孩子看病时会发出v[i]的叫声,他后面的那个人的自信心(不是p[i+1])会减少v[i],再后面一个会减少v[i]-1,如此下去直到声音减弱为0.若某个人的自信心小于0,则他哭着跑回家,他身后的所有人会减掉d[i] 的自信. 题解:直接模拟很困难,有一个想法是将逃跑的孩子的声音和将他吓跑的(正在接诊的)声音叠加成s,

CodeForces 548B Mike and Fun (模拟)

题意:给定一个n*m的矩阵,都是01矩阵,然后每次一个询问,改变一个格的值,然后问你最大有数是多少. 析:就是按他说的模拟,要预处理,只要把每行的最大值记下来,当改变时,再更新这一行的最大值. 代码如下: #include<bits/stdc++.h> using namespace std; const int maxn = 500 + 5; int a[maxn][maxn]; int num[maxn]; int main(){ int n, m, q, x, y; while(cin

Codeforces Good Bye 2016 D 模拟搜索?

给出烟花的爆炸方式和爆炸次数 问最后有多少个格子会被炸到 如果dfs的话会超时... 利用模拟每一层来搜索..? 思想就是一开始有一个爆炸点向上 然后模拟完第一段 会产生一个爆炸点 朝两个方向 就用vector来存 每一层都处理一段的爆炸点 产生新一段的爆炸点 因为5*30=150 所以图建300就可以了 300 * 300 * 30的时间复杂度 但是常数很大..不过无所谓啦.. 需要注意的是 一个爆炸点可能会同时出现两次朝同一个方向开始爆炸的烟花 这个是没有意义的 所以拿一个数组来记录 不然最