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. Mysql 数据库存储的原理??

    储存过程是一个可编程的函数,它在数据库中创建并保存.它可以有 SQL 语句和一些特殊的控制结 构组成.当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有用的.数据库中的 ...

  2. nodejs爬虫编码问题

    最近再做一个nodejs网站爬虫的项目,但是爬一些网站的数据出现了中文字符乱码的问题.查了一下,主要是因为不是所有的网站的编码格式都是utf-8,还有一些网站用的是gb2312或者gbk的编码格式.所 ...

  3. Python之路-pandas包的详解与使用

    什么是pandas pandas是一种Python数据分析的利器,是一个开源的数据分析包,最初是应用于金融数据分析工具而开发出来的,因此pandas为时间序列分析提供了很好的支持.pandas是PyD ...

  4. openstack stein部署手册 8. neutron-api

    # 建立数据库用户及权限 create database neutron; grant all privileges on neutron.* to neutron@'localhost' ident ...

  5. Maya2019下载安装与激活

    目录 1. 更多推荐 2. 下载地址 2.1. OneDrive 2.2. Window (64位) 2.3. MAC_OSX 3. 安装激活教程 1. 更多推荐 其他Maya版本的下载与激活:htt ...

  6. 读书笔记三、pandas之重新索引

  7. springboot2.0整合redis的发布和订阅

    1.Maven引用 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  8. springboot 中的 thymeleaf 页面进阶小知识

    我们做好了登录页,那设计一个场景吧,比如我们登录后跳转到公司主页,想从公司主页再跳转到员工列表页: 这样的场景我们该如何实现,首先要知道一些基础知识,就是SpringBoot的一些关于请求的架构知识: ...

  9. JAVA中的反射机制 详解

    主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...

  10. 关于scikit-learn

    机器学习scikit-learn scikit-learn官网学习资料非常丰富,完全可以自学: http://scikit-learn.org/ 目前就以scikit-learn为主要工具学习mach ...