[Poj] A new stone game (Game Theory)

Source: Internet
Author: User

Http://poj.org/problem? Id = 1740

The general idea is that for N piles of stones, each Heap has several, and the two take turns, each operation is divided into two steps, the first step is to remove at least one from a pile, step 2 (may be omitted) divide Part of the remaining stone in the heap to some other heap. In the end, no one can afford to lose.

It's amazing to read the question.

First, let's analyze:

When there is only one heap, the first hand wins, just get it all at once

When there are two stacks, there are two situations: one is that the two stacks are the same, and the first hand must be the same, because no matter how the first hand gets it, the latter hand will always have a way to balance the two stacks again; however, if the two stacks are different, the first hand will win, because the first hand will always have a way to balance the two stacks first, and then it will be like a situation.

When there are three stacks, there is always a way to balance them with two. (Add the most heap parts, and then fill the remaining two)

When an even number of heaps exist and are sorted from small to large, the two are equal (I .e., 1 and 2 are equal, 3 and 4 are equal, and 5 and 6 are equal ...), the first hand must lose, because no matter how the first hand gets the first hand, the second hand can always balance the number of heap to an even number and the two are equal.

When there is an odd number of heap, the first hand will win, because after sorting all the heap from small to large, the largest heap will surely be able to fill up the corresponding heap balance one by one, this is the fourth case (map the heap height difference of the front edge to the Y axis, and we can find that the sum must be smaller than the top heap. Note that the heap must be smaller than the top heap, so the heap must be balanced)

When there is an even number of heaps and there is a one-to-one heap imbalance after sorting from small to large, the first hand will win, because the first hand can always balance these even numbers first and become the fourth case, (After removing at least one of the largest heaps, move the remaining stones and the shortest stones with the lowest heap height to another heap for balance! In the same case, map to the Y axis)

Therefore, we only need to judge whether the stone corresponding to each other is balanced when the even number is heap. If yes, the first hand will lose. If not, the first hand will win.

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=1000;int a[N], n;int main() {while(~scanf("%d", &n) && n) {rep(i, n) read(a[i]);if(n&1) puts("1");else {bool flag=0;sort(a, a+n);for(int i=0; i<n; i+=2) if(a[i]!=a[i+1]) { flag=1; break; }if(flag) puts("1");else puts("0");}}return 0;}

 

 

 

 

Description

Alice and Bob decide to play a new stone game. at the beginning of the game they pick N (1 <= n <= 10) piles of stones in a line. alice and Bob move the stones in turn.
At each step of the game, the player choose a pile, remove at least one stones, then freely move stones from this pile to any other pile that still has stones.
For example: N = 4 and the piles have (3,1, 4,2) stones. If the player chose the first pile and remove one. Then it can reach the follow states.
2 1 4 2
1 2 4 2 (move one stone to pile 2)
1 1 5 2 (move one stone to pile 3)
1 1 4 3 (move one stone to pile 4)
0 2 5 2 (move one stone to pile 2 and another one to pile 3)
0 2 4 3 (move one stone to pile 2 and another one to pile 4)
0 1 5 3 (move one stone to pile 3 and another one to pile 4)
0 3 4 2 (move two stones to pile 2)
0 1 6 2 (move two stones to pile 3)
0 1 4 4 (move two stones to pile 4)
Alice always moves first. Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.

Input

The input contains several test cases. the first line of each test case contains an integer number N, denoting the number of piles. the following n integers describe the number of stones in each pile at the beginning of the game, you may assume the number of stones in each pile will not exceed 100.
The last test case is followed by one zero.

Output

For each test case, if Alice win the game, output 1, otherwise output 0.

Sample Input

32 1 321 10

Sample output

10

Source

[Email protected]

[Poj] A new stone game (Game Theory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.