zoukankan      html  css  js  c++  java
  • 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    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.

    Examples
    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:

    【题目链接】:http://codeforces.com/contest/546/problem/C

    【题解】

    用vector+reverse来模拟这个过程
    然后用map来判断有没有出现循环节;
    (用队列,然后轮数到了一个很大的数字还没出结果就直接跳出,这样也可以)
    ps:貌似循环1e8次也不会超1s的时限.

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //const int MAXN = x;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    vector <int> a,b;
    int n,r=0;
    int k1,k2;
    map <pair< vector<int>,vector <int> > ,int> dic;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);
        rei(k1);
        rep1(i,1,k1)
            {
                int x;
                rei(x);
                a.pb(x);
            }
        rei(k2);
        rep1(i,1,k2)
            {
                int x;
                rei(x);
                b.pb(x);
            }
        dic[mp(a,b)] = 1;
        dic[mp(b,a)] = 1;
        while (true)
        {
            r++;
            reverse(a.begin(),a.end());reverse(b.begin(),b.end());
            int A = a.back(),B = b.back();
            a.pop_back();b.pop_back();
            reverse(a.begin(),a.end());reverse(b.begin(),b.end());
            if (A<B)
            {
                b.pb(A);
                b.pb(B);
            }
            else//A>B
            {
                a.pb(B);
                a.pb(A);
            }
            if (dic[mp(a,b)])
            {
                puts("-1");
                return 0;
            }
            if (a.empty())
            {
                printf("%d %d
    ",r,2);
                return 0;
            }
            else
                if (b.empty())
                {
                    printf("%d %d
    ",r,1);
                    return 0;
                }
            dic[mp(a,b)] = 1;
            dic[mp(b,a)] = 1;
        }
        return 0;
    }
  • 相关阅读:
    hadoop集群的搭建
    EclipseAndroid打包签名发布安装失败闪退运行不了
    [目录]C#学习笔记
    [目录]搭建一个简单的WebGIS应用程序
    实现DataTables搜索框查询结果高亮显示
    解决将Excel表导入到SQL Server数据库时出现Text was truncated or one or more characters had no match in the target code错误
    将展示内容(div、iframe)放在Expand控件中
    [C#学习笔记1]用csc.exe和记事本写一个C#应用程序
    选中FeatureLayer元素并高亮显示
    在地图中调用显示FeatureLayer并进行render、popupTemplate、添加图例等相关内容的设置
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626761.html
Copyright © 2011-2022 走看看