题目
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.
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).
Output
For each game, print on a single line the name of the winner, “T” or “HL” (without quotes)
题意:
这是一道博弈题,先手为T,后手为HL。现在有一个拿石子的游戏,每人每次可以从石子堆中取出一个石子,这个石子堆是除了上一次选手拿到那个石子堆之外的石子堆,言外之意就是两个人不能拿相同的石子堆里面的石子,最后没有石子可拿的选手输掉比赛。输出赢家
题解:
抱着试一发的心态去的,没想到过了。
以下几种情况先手必胜:
1、石子数总和为奇数
2、只有一堆石子的时候
3、其中有一堆石子中的石子数大于剩下所有石子堆中石子数的总和,这样可以将对手消磨到死
其余情况后手胜
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;
}