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;
}
相關文章
相關標籤/搜索