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;
}
posted @ 2020-07-22 13:47  Luglucky  阅读(178)  评论(0编辑  收藏  举报