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

    题意描述:游戏双方各有一些沓牌,牌上都有一个各不相同的值,每一轮取出最上面的牌进行比较,大的一方把对方的牌压在自己的一沓牌的下面,然后再把自己的这张牌再次压在最下面,就这样一直进行下去,直到有一方没有牌或者不会出现没有牌的情况为止。

    算法分析:一看这题就知道没有什么算法,在判断游戏会一直进行下去的时候,怎么样来标记之前出现过的一沓牌的顺序,如果之后游戏双方均同时出现之前的一沓牌的顺序的状态,那么这次游戏就会一直进行下去,不会停止,此时想到一沓牌的顺序哈希成为一个值,这样就可以标记一下啦。

    可惜昨晚做的时候真心日天了,哈希的方法出现很大的错误,导致电脑没电的时候也没有找到错误,难道我现在真不适合熬夜了吗?fuck,fuck,damn,damn。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define inf 0x7fffffff
     8 using namespace std;
     9 const int mod=3041;
    10 
    11 int n,k1,k2;
    12 int an[12],bn[12],cn[24],dn[24];
    13 int vis[3050][3050];
    14 
    15 int main()
    16 {
    17     while (scanf("%d",&n)!=EOF)
    18     {
    19         int sum=0,sum2=0;
    20         memset(vis,0,sizeof(vis));
    21         scanf("%d",&k1);
    22         for (int i=1 ;i<=k1 ;i++)
    23         {
    24             scanf("%d",&an[i]);
    25             cn[i]=an[i];
    26             sum += an[i]*an[i]*i;
    27         }
    28         scanf("%d",&k2);
    29         for (int i=1 ;i<=k2 ;i++)
    30         {
    31             scanf("%d",&bn[i]);
    32             dn[i]=bn[i];
    33             sum2 += bn[i]*bn[i]*i;
    34         }
    35         sum %= mod ;sum2 %= mod ;
    36         vis[sum][sum2]=1;
    37         sum=sum2=0;
    38         int flag=0,ans=0;
    39         int i=1,j=1,c=k1,d=k2;
    40         while (i<=c && j<=d)
    41         {
    42 //            cout<<"debug
    ";
    43 //            for (int u=i ;u<=c ;u++) cout<<cn[u]<<" ";
    44 //            cout<<endl;
    45 //            for (int u=j ;u<=d ;u++) cout<<dn[u]<<" ";
    46 //            cout<<"debug end"<<endl;
    47             if (i!=1 && j!=1)
    48             {
    49                 flag=0;
    50                 sum=0;
    51                 for (int u=i ;u<=c ;u++) sum += cn[u]*cn[u]*(u-i+1);
    52                 sum2=0;
    53                 for (int u=j ;u<=d ;u++) sum2 += dn[u]*dn[u]*(u-j+1);
    54                 sum %= mod ;sum2 %= mod ;
    55                 if (vis[sum][sum2])
    56                 {
    57                     flag=1;
    58                     break;
    59                 }
    60                 vis[sum][sum2]=1;
    61             }
    62             if (cn[i]>dn[j])
    63             {
    64                 cn[++c]=dn[j] ;cn[++c]=cn[i] ;
    65                 i++ ;j++ ;
    66                 ans ++ ;
    67             }
    68             else
    69             {
    70                 dn[++d]=cn[i] ;dn[++d]=dn[j] ;
    71                 i++ ;j++ ;
    72                 ans ++ ;
    73             }
    74         }
    75         if (flag) printf("-1
    ");
    76         else if (i>c) printf("%d 2
    ",ans);
    77         else printf("%d 1
    ",ans);
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    C#和Sql Server 2005中时间的最大值和最小值
    Windows Server 2008 R2 With SP1简体中文版 + 破解补丁
    Merge窗体的制作
    SqlServer2008R2卸载
    Highlighter(高亮控件的边框)
    如何删除window.old文件
    如何全屏WinForm的窗体
    验证时出错。HRESULT = '8000000A'
    ReflectionLabel(倒影控件)
    windows 2003和server 2008 取消对网站的安全检查/去除添加信任网站
  • 原文地址:https://www.cnblogs.com/huangxf/p/4523794.html
Copyright © 2020-2023  润新知