2018CCPC桂林站JStone Game

题目描述

Alice and Bob are always playing game! The game today is about taking out stone from the stone piles in turn.
There are n piles of stones, and the i-th pile contains A[i] stones.
As the number of stones in each pile differs from its neighbor’s, they determine to take out exactly one stone from one of them in one turn without breaking that property. Alice goes first.
The player who cannot take stone will lose the game.
Now you have to determine the winner given n numbers representing the piles, if they both play optimally.
You should notice that even a pile of 0 stone is still regarded as a pile!

输入

The first line of input file contains an integer T (1≤T≤100), describing the number of test cases.
Then there are 2×T lines, with every two lines representing a test case.
The first line of each test case contains only one integer n (1≤n≤105) as described above.
The second line of that contains exactly n numbers, representing the number of stone(s) in a pile in order.
All these numbers are ranging in [0,109].
It is guaranteed that the sum of n in all cases does not exceed 106.

输出

You should output exactly T lines.
For each test case, print Case d: (d represents the order of the test case) first, then print the name of winner, Alice or Bob .

样例输入
复制样例数据

2
2
1 3
3
1 3 1
样例输出

扫描二维码关注公众号,回复: 6143391 查看本文章

Case 1: Alice
Case 2: Bob
 

提示

Sample 1:
There is a possible stone-taking sequence: (1,3)-> (1,2)-> (0,2)-> (0,1)
Here is an invalid sequence: (1,3)-> (1,2)-> (1,1)-> (0,1). Though it has the same result as the first sequence, it breaks that property “the number of stones in each pile differs from its neighbor’s”.
思路来自于2018CCPC桂林站题解(D G H J L)

  题目大意:n堆石子,保证相邻两堆不等。A和B玩游戏,轮流从这n堆中,任意拿走一颗石子,但需要保证拿走第i堆的一颗石子后,第i堆的石子不能和他相邻堆相等。谁无法拿石子时,谁就输。问谁能赢?

  因为相邻位置的数目不能相同,如果对于两个位置x,y的石头有a[x]>a[y],那么一直有a[x]>a[y],因为每个只能取一个,多那个是不可能越过少那个的。所以整个取石头的过程中,序列的单调性是不变的,那么当不可以再取时,序列中的极小值处肯定取为0了,我们就可以从极小值处向两边的峰值扩展,逐渐递增。而当两个极小值的峰值相遇时,就说明这个位置的值大于它两边的值,根据前面的a[x]>a[y]是恒定不变的,那么这个位置的最后的值就应该为它两边更大的那个+1。处理完所有极小值之后,就得到了不能再取时的序列,它和原序列的差就是总共能取的石头数,如果是奇数那么是alice赢,反正bob。详情见代码。

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 typedef long long ll;
 5 const int N=100118,inf=1e9+118;
 6 int a[N],b[N];
 7 int main()
 8 {
 9     int t=1,T,n;
10     scanf("%d",&T);
11     while(t<=T)
12     {
13         scanf("%d",&n);
14         ll sum=0;
15         for(int i=1;i<=n;i++)
16         {
17             scanf("%d",&a[i]);
18             sum+=a[i];
19             b[i]=inf;
20         }
21         printf("Case %d: ",t++);
22         if(n==1)//1的时候奇偶性特判 
23         {
24             if(sum&1)
25                 printf("Alice\n");
26             else
27                 printf("Bob\n");
28             continue;
29         }
30         a[0]=a[n+1]=inf;//两端也可能是极小值,在序列的两边增加个尽可能大的值进行处理 
31         for(int i=1;i<=n;i++)
32             if(a[i]<a[i-1]&&a[i]<a[i+1])//先找到极小值设为0 
33                 b[i]=0;
34         for(int i=1;i<=n;i++)
35         {
36             if(!b[i])
37             {    //从极小值向两边递增 
38                 for(int j=i+1;j<=n&&a[j]>a[j-1];j++)
39                     b[j]=b[j-1]+1;
40                 for(int j=i-1;j>=1&&a[j]>a[j+1];j--)
41                     if(a[j]>a[j-1]&&a[j]>a[j+1])//当两个极小值的峰值相遇 
42                         b[j]=max(b[j-1],b[j+1])+1;
43                     else
44                         b[j]=b[j+1]+1;
45             }
46         }
47         for(int i=1;i<=n;i++)
48             sum-=b[i];
49         if(sum&1)
50             printf("Alice\n");
51         else
52             printf("Bob\n");
53     }
54     return 0; 
55 }
不够聪明怎么办

猜你喜欢

转载自www.cnblogs.com/LMCC1108/p/10822928.html