Codeforces Round #666 (Div. 2) ------ Stoned Game

题目

T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has ai stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.ios

Input
The first line of the input contains a single integer t (1≤t≤100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1≤n≤100) — the number of piles.
The second line contains n integers a1,a2,…,an (1≤ai≤100).c++

Output
For each game, print on a single line the name of the winner, “T” or “HL” (without quotes)web

题意:

这是一道博弈题,先手为T,后手为HL。如今有一个拿石子的游戏,每人每次能够从石子堆中取出一个石子,这个石子堆是除了上一次选手拿到那个石子堆以外的石子堆,言外之意就是两我的不能拿相同的石子堆里面的石子,最后没有石子可拿的选手输掉比赛。输出赢家svg

题解:

抱着试一发的心态去的,没想到过了。
如下几种状况先手必胜:
一、石子数总和为奇数
二、只有一堆石子的时候
三、其中有一堆石子中的石子数大于剩下全部石子堆中石子数的总和,这样能够将对手消磨到死

其他状况后手胜spa

AC代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
#include<math.h>
using namespace std;
#define ll long long
const ll inf = 1e15;
const int N = 1e2 + 15;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int t; cin >> t;
	int a[N];
	while (t--) {
		int n; cin >> n;
		int sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			sum += a[i];
		}
		sort(a, a + n);
		if (a[n - 1] > (sum - a[n - 1]) || n == 1 || sum % 2 == 1)
			cout << "T" << endl;
		else
			cout << "HL" << endl;
	}
	return 0;
}
相关文章
相关标签/搜索