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. vue+element Form键盘回车事件页面刷新解决

    问题描述:如下代码所示,使用element-ui 中的el-form组件对table进行条件查询,当查询条件仅有一项时,使用@keyup.enter.native事件绑定回车事件,出现点击回车后,浏览 ...

  2. 靶形数独 (dfs+预处理+状态压缩)

    #2591. 「NOIP2009」靶形数独 [题目描述] 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们 ...

  3. Codeforces - 6E - Exposition - 尺取

    https://codeforc.es/problemset/problem/6/E 既然可以多个log,那就直接map伺候.尺取之后要查询区间里面的最大值和最小值的差.众所周知尺取的时候要是不是有序 ...

  4. MySQL 保存镜像实战操作( 拷贝方法 )

    查看数据保存的位置 docker inspect --format='{{.Mounts}}' mxg_mysql 容器路径为:`/var/lib/mysql` ,宿主机数据保存在: /var/lib ...

  5. 后台PDF返回Base64,前台接收预览

    读取已存在的PDF文件,path为绝对路径 string base64String = "";byte[] buffer=null; using (FileStream fs = ...

  6. python函数传参和返回值注意事项

    函数传参 空参数 定义函数时括号里面没有形参,调用时不用传参. def func(): print('null para.') # 调用 func() 位置传参 规定形参的数量,调用时必须传递相同数量 ...

  7. Nginx安装与配置-Centos7

    Nginx是一款高性能免费开源网页服务器,也可用于反向代理和负载均衡服务器.该软件由伊戈尔·赛索耶夫于2004年发布,2019年3月11日,Nginx被F5 Networks以6.7亿美元收购.201 ...

  8. sass @import 规则

    @import 根据文件名引入. 默认情况下,它会寻找 Sass 文件并直接引入, 但是,在少数几种情况下,它会被编译成 CSS 的 @import 规则: 如果文件的扩展名是 .css. 如果文件名 ...

  9. pycharm中能运行,但是往往py都要放到服务器上去跑,问题来了

    py文件在linux上运行,导包错误: 在py文件中添加项目的根目录: import sys sys.path.append('项目路径') sys.path.append(os.path.dirna ...

  10. CSS中用 opacity、visibility、display 属性将 元素隐藏 的 对比分析

    说明 opacity 用来设置透明度 display 定义建立布局时元素生成的显示框类型 visibility 用来设置元素是否可见. opacity.visibility.display 这三个属性 ...