zoukankan      html  css  js  c++  java
  • Codeforces 1382B

    B. Sequential Nim

    time limit per test1 second
    memory limit per test256 megabytes
    input standard input
    output standard output
    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
    input
    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
    output
    First
    Second
    Second
    First
    First
    Second
    First
    Note
    In the first test case, the first player will win the game. His winning strategy is:

    The first player should take the stones from the first pile. He will take 1 stone. The numbers of stones in piles will be [1,5,4].
    The second player should take the stones from the first pile. He will take 1 stone because he can’t take any other number of stones. The numbers of stones in piles will be [0,5,4].
    The first player should take the stones from the second pile because the first pile is empty. He will take 4 stones. The numbers of stones in piles will be [0,1,4].
    The second player should take the stones from the second pile because the first pile is empty. He will take 1 stone because he can’t take any other number of stones. The numbers of stones in piles will be [0,0,4].
    The first player should take the stones from the third pile because the first and second piles are empty. He will take 4 stones. The numbers of stones in piles will be [0,0,0].
    The second player will lose the game because all piles will be empty.

    题目大意:

    两个人玩石子游戏,有这样几堆石子,从第一堆开始取,最多只能拿走这一堆的所有石子,而最少要拿走一颗,不能操作的算输。给出石子的堆数以及每堆石子的个数,两人均使用最佳策略,询问先手胜还是后手胜利。

    解题思路:

    每个人只能拿走1颗到一整堆,假设有两堆石子,第一堆n个,第二堆任意,那么只需要从第一堆拿走n-1个,后手只能拿1个,然后再把第二堆全部拿走即可获胜。我们只需要判断从1开始连续的1堆石子的堆数即可,因为这堆石子前面没有石子或者只有数量为1个石子的堆是时,只能拿1个,那么必胜就会变为必败,比如 1 5 5,只能拿1,拿完就会必败,但如果1的的石子在中间,比如5 1 1 5 时,可以先拿4个,后手1个,轮到5时还是必胜,所以只需要考虑从头开始连续的1的堆数就可以了。
    从头开始,当连续为1的堆数为奇数时,后手胜,因为先手一定会变成必败态,反之则先手胜,特判一下,如果n堆全是1,那么奇数先手胜,偶数后手胜,AC代码:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <queue>
    using namespace std;
    const int mod = 1e9 + 7;
    const int N = 1e5 + 50;
    const int inf = 0x3f3f3f3f;
    typedef long long ll;
    typedef pair<int, int> pii;
    int main()
    {
    	int t;
    	cin >> t;
    	while (t--)
    	{
    		int n;
    		cin >> n;
    		vector<int > v(n);
    		for (auto &it : v)
    		  cin >> it;
    		int k = 0;
    		while(k < n && v[k] == 1) k++;//从头开始统计连续为1的堆数
    		if (k == n)
    		  cout << (k & 1 ? "First" : "Second") << endl;
    		else
    		  cout << (k & 1 ? "Second" : "First") << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    Beta阶段冲刺---Day1
    网络15软工个人作业5——软件工程总结
    软工网络15个人作业4——alpha阶段个人总结
    软件工程个人作业3——集大通APP案例分析
    软件工程网络15结对编程作业
    软件工程网络15个人阅读作业2-提出问题
    软工网络15个人阅读作业1
    Java课程设计—学生成绩管理系统(201521123002 林楚虹)
    201521123002《Java程序设计》第14周学习总结
    201521123002 《Java程序设计》第13周学习总结
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294189.html
Copyright © 2011-2022 走看看