zoukankan      html  css  js  c++  java
  • 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 #include <cmath>
    12 #include <queue>
    13 using namespace std;
    14 
    15 const int MAXN = 20;
    16 const int INF = 0x3f3f3f3f;
    17 int x[MAXN];
    18 queue<int> Q1;
    19 queue<int> Q2;
    20 
    21 int main(void)        //Codeforces Round #304 (Div. 2) C. Soldier and Cards
    22 {
    23     int n, m1, m2;
    24     while (scanf ("%d", &n) == 1)
    25     {
    26         while (!Q1.empty ())    Q1.pop ();
    27         while (!Q2.empty ())    Q2.pop ();
    28 
    29         int x;
    30         scanf ("%d", &m1);
    31         for (int i=1; i<=m1; ++i)
    32         {
    33             scanf ("%d", &x);    Q1.push (x);
    34         }
    35 
    36         scanf ("%d", &m2);
    37         for (int i=1; i<=m2; ++i)
    38         {
    39             scanf ("%d", &x);    Q2.push (x);
    40         }
    41 
    42         int cnt = 0;
    43         while (!Q1.empty () && !Q2.empty ())
    44         {
    45             int x = Q1.front ();    int y = Q2.front ();
    46             Q1.pop ();    Q2.pop ();
    47             if (x < y)    {Q2.push (x);    Q2.push (y);}
    48             else    {Q1.push (y);    Q1.push (x);}
    49             ++cnt;
    50             if (cnt == 10000)    break;
    51         }
    52 
    53         if (cnt == 10000)    puts ("-1");
    54         else
    55         {
    56             printf ("%d %d
    ", cnt, (Q1.empty ()) ? 2 : 1);
    57         }
    58     }
    59 
    60     return 0;
    61 }
    62 
    63 /*
    64 4
    65 2 1 3
    66 2 4 2
    67 3
    68 1 2
    69 2 1 3
    70 */
    编译人生,运行世界!
  • 相关阅读:
    Asp.NET调用有道翻译API
    JSON C# Class Generator ---由json字符串生成C#实体类的工具
    让jQuery的contains方法不区分大小写
    javascript parseUrl函数(来自国外的获取网址url参数)
    typescript
    webpack 第二部分
    express node 框架介绍
    webpack 最新版
    es6 字符串 对象 拓展 及 less 的语法
    es6 的数组的方法
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4530591.html
Copyright © 2011-2022 走看看