Stone 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

样例输出

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”.

以下题意和题解来源自https://blog.csdn.net/winter2121/article/details/89763995

题意

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

题解

设a[x]与a[y]相邻,若a[x]>a[y],则永远不会出现a[x]<=a[y]的情况。于是整个序列的单调性不会发生变化。

找出所有的极小值点(两端特判),这些点最后一定能拿成0。然后从极小值点往两边爬,爬到封顶,爬的时候让a[i]变成前一个+1即可。也就是保持单调性的前提下,尽量降低序列的值。  令sum = 原序列的和 - 现序列的和,则sum为奇数时A赢,否则B赢

原博主的题解写的很好,我在原博主的基础上精简了部分代码,增添了些注释方便理解

代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
#define LL long long
#define pb push_back
#define mp make_pair
#define P pair<double,double>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define mod 1e9+7
#define INF 0x3f3f3f3f
#define N 1005
const int maxn = 1e6+10;
int a[maxn],b[maxn];
int t,n;
int kase = 0;
int main()
{
sca(t);
while(t--)
{
sca(n);
ll sum = 0;
rep(i,1,n+1)
{
sca(a[i]);
sum+=a[i];
}
printf("Case %d: ",++kase);
if(n == 1) //如果只有一个数
{
puts(sum % 2 ? "Alice" : "Bob");
continue;
}
int cnt = 0;
a[0]= a[n+1] = -1; //方便下面操作
//先处理单调的两端,这样后面从中间开始的操作才是正确的
for(int i = 1;i < n && a[i]<a[i+1]; i++) a[i] = a[i-1] + 1;
for(int i = n;i > 1 && a[i]<a[i-1]; i--) a[i] = a[i+1] + 1;
rep(i,2,n)
{
if(a[i] <a[i-1] && a[i]<a[i+1])//位于中间的极小值
{
a[i] = 0; //直接把他变0;
for(int j = i-1; j > 1 && a[j] < a[j-1]; j--) //如果保持单调
a[j] = a[j+1] + 1;
for(int j = i+1; j < n && a[j] < a[j+1]; j++)
a[j] = a[j-1] + 1;
}
}
rep(i,1,n+1)
{ //因为在上面的操作中我们没有处理头和尾,所以在这里处理
if(i == 1 && a[1] > a[2]) a[1] = a[2] + 1;
else if(i == n && a[n] > a[n-1]) a[n] = a[n-1]+1;
else if(a[i] > a[i-1] && a[i] > a[i+1])
a[i] = max(a[i-1],a[i+1])+1; //如果比旁边两个都大,那只能减到大者+1的数
} rep(i,1,n+1)
{
sum-=a[i];//操作次数 = 原序列-操作后序列
//printf("%d ",a[i]);
}
//puts("");
//prl(sum);
puts(sum % 2 ? "Alice" : "Bob");
}
return 0;
}

upc组队赛17 Stone Game【极小值】的更多相关文章

  1. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  2. upc组队赛17 Bits Reverse【暴力枚举】

    Bits Reverse 题目链接 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ...

  3. upc组队赛16 GCDLCM 【Pollard_Rho大数质因数分解】

    GCDLCM 题目链接 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some int ...

  4. upc组队赛15 Supreme Number【打表】

    Supreme Number 题目链接 题目描述 A prime number (or a prime) is a natural number greater than 1 that cannot ...

  5. upc组队赛3 Congestion Charging Zon【模拟】

    Congestion Charging Zon 题目描述 Tehran municipality has set up a new charging method for the Congestion ...

  6. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

  7. upc组队赛1 过分的谜题【找规律】

    过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...

  8. upc组队赛1 不存在的泳池【GCD】

    不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...

  9. upc组队赛1 黑暗意志【stl-map】

    黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...

随机推荐

  1. jmeter 线程数—请求数详解

    一个性能测试请求负载是基于一个线程组完成的.一个测试计划必须有一个线程组.测试计划添加线程组非常简单.在测试计划右键弹出下拉菜单(添加-->Threads(Users)--->线程组)中选 ...

  2. hdu 3333 离线线段树 + 思维/树状数组 /在线主席树

    #include<iostream> #include<cstdio> #include<string> #include<cmath> #includ ...

  3. Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍

    原文:Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本 ...

  4. 帝国CMS链接域名重写、伪静态处理

    需求:将 www.abc.com/e/tags?tagsid=1 改写成  www.abc.com/softlink/1.html形式: 环境:Windows服务器: 路径:D:/web/www.ab ...

  5. jQuery中attr()和prop()及removeAttr()和removeProp()的区别

    在jquery1.6之前的所有版本中设置和获取元素属性(固有属性和自定义属性)都使用attr方法,后来单独把设置和获取元素固有属性单独做成了prop()方法. 一般来说: 对于HTML元素本身就带有的 ...

  6. Spark MLlib机器学习(一)——决策树

    决策树模型,适用于分类.回归. 简单地理解决策树呢,就是通过不断地设置新的条件标准对当前的数据进行划分,最后以实现把原始的杂乱的所有数据分类. 就像下面这个图,如果输入是一大堆追求一个妹子的汉子,妹子 ...

  7. ASE Beta Sprint - backend scrum 1

    本次scrum于2019.12.2与前端组和模型组一起在sky garden进行,持续50分钟. 参与人: Xin Kang, Zhikai Chen, Lihao Ran, Hao Wang 请假: ...

  8. Raven2

     Raven2实验 0x01 寻找IP 本机IP:56.130 1. 使用 nmap -sn "ip6"#主机发现(不进行端口扫描) https://cloud.tencent.c ...

  9. nodejs 文件读写

    文件读取: //例如: fs.readFile 就是用来读取文件的 //1. 使用require方法来加载 fs 核心模块 var fs = require('fs'); /* *2. 读取文件 * ...

  10. Jupyter配置工作路径

    在修改之前,C:\Users\Administrator\ .jupyter 目录下面只有一个“migrated”文件. 打开命令窗口(运行->cmd),进入python的Script目录下输入 ...