1. 程式人生 > >AtCoder - 1999 Candy Piles

AtCoder - 1999 Candy Piles

nat HR brush mos CI ati () pac 問題

Problem Statement

There are N piles of candies on the table. The piles are numbered 1 through N. At first, pile i contains ai candies.

Snuke and Ciel are playing a game. They take alternating turns. Snuke goes first. In each turn, the current player must perform one of the following two operations:

  1. Choose a pile with the largest number of candies remaining, then eat all candies of that pile.
  2. From each pile with one or more candies remaining, eat one candy.

The player who eats the last candy on the table, loses the game. Determine which player will win if both players play the game optimally.

Constraints
  • 1N105
  • 1ai109
Input

The input is given from Standard Input in the following format:

N
a1 a2  aN
Output

If Snuke will win, print First. If Ciel will win, print Second.

Sample Input 1
2
1 3
Sample Output 1
First

At the beginning of the game, pile 2 contains the most candies. If Snuke eats all candies of this pile, Ciel has no choice but to eat the last candy.

Sample Input 2
3
1 2 1
Sample Output 2
First

If Snuke eats one candy from each pile, Ciel is again left with the last candy.

Sample Input 3
3
1 2 3
Sample Output 3
Second

(假設高度有序,從左到右遞減)
可以把問題轉化成,初始在(0,0),有n個矩形拼成的凸多邊形,第i個矩形是的左下角是(i-1,0),右上角是(i,h[i])。那麽遊戲就相當於每次只能向右或者向上走,不能走出多邊形,問先手是否必勝。
可以先畫一畫1*1的小正方形的情況,可以發現不論右上角是什麽狀態,左下角一定和它的狀態一樣 (右上角必敗比較好發現,必勝的話要多畫幾個其他點的狀態)。
於是就可以直接暴力做了2333

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=100005;

int a[N],n;
bool sg;

inline void solve(){
	for(int i=1;i<=n;i++) if(i+1>a[i+1]){
		for(int j=i+1;a[j]==i;j++) sg^=1;
		sg|=(a[i]-i)&1,puts(sg?"First":"Second");
		break;
	}
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	sort(a+1,a+n+1),reverse(a+1,a+n+1);
	
	solve();
	
	return 0;
}


AtCoder - 1999 Candy Piles