zoukankan      html  css  js  c++  java
  • Codeforces #658 Problem B

    题面

    There are n piles of stones, where the i-th pile has ai stones. Two people play a game, where they take alternating turns removing stones.

    In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game.

    Input
    The first line contains a single integer t (1≤t≤1000) — the number of test cases. Next 2t lines contain descriptions of test cases.

    The first line of each test case contains a single integer n (1≤n≤105) — the number of piles.

    The second line of each test case contains n integers a1,…,an (1≤ai≤109) — ai is equal to the number of stones in the i-th pile.

    It is guaranteed that the sum of n for all test cases does not exceed 105.

    Output
    For each test case, if the player who makes the first move will win, output "First". Otherwise, output "Second".

    Example
    inputCopy
    7
    3
    2 5 4
    8
    1 1 1 1 1 1 1 1
    6
    1 2 3 4 5 6
    6
    1 1 2 1 2 2
    1
    1000000000
    5
    1 2 2 1 1
    3
    1 1 1
    outputCopy
    First
    Second
    Second
    First
    First
    Second
    First

    思路

    就是一个按顺序的尼姆博弈。但和原策略完全不一样。首先我们是可以把一堆石子全部拿完的,所以先手的策略是最好是最后一堆的选择权在我的手上,这样我直接拿光就赢了,后手也是一样,模拟几组情况你会发现,最终我们要判断的就是这个决定权掌握在谁手上。

    代码实现

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int maxn=101000;
    
    int a[maxn];
    
    int main () {
       int  t;
       cin>>t;
       while (t--) {
          int n;
          cin>>n;
          for (int i=0;i<n;i++) cin>>a[i];
          int cnt=0;
          if (n==1) {
              cout<<"First"<<endl;
              continue;
          }
          else {
              while (a[cnt]==1&&cnt<n) cnt++;
              
              if (cnt==n) {
                  if (cnt%2==0) cout<<"Second"<<endl;
                  else cout<<"First"<<endl;
              }
              else {
                  if (cnt%2==0) cout<<"First"<<endl;
                  else cout<<"Second"<<endl;
              }
              
          }
    
       }       
    
        return 0;
    }
    
  • 相关阅读:
    P1967 货车运输【最大生成树+倍增LCA】!!!
    P1991 无线通讯网【kruskal】
    P2872 [USACO07DEC]Building Roads S【kruskal】
    最小生成树
    树的直径
    树的重心
    今日英语单词小结
    项目生命周期
    反射reflect(框架的基石),动态导入小技巧 | 元类 | 单例设计模式
    OOP的三大特征之多态 | 面向对象高级知识,内置魔法函数,点语法和[ ]取值的实现,运算符重载,迭代器协议,上下文管理
  • 原文地址:https://www.cnblogs.com/hhlya/p/13360237.html
Copyright © 2011-2022 走看看