题意:给你一串非负整数,可以将一个非零数减1,加到相邻的数字上,要使其中所有最大数字的和最小。

题解:模拟可以过。也可以分析,可以要减少最大数字和,如果最大数字出现大于等于3次,可以把最大数字加一,或者把某个最大数字减一,最大数字出现减少一次。但是要注意一些特殊情况,下面详述。

先扫一遍,如果最大数字为0,那么输出0,如果最大数字为1,那么如果可以合并成2的话,那么就输出2,否则输出数字和sum。否则,看最大数字出现次数,如果为一次,那么检查,周围是否有比它小3的数字,(如果只是比它小2的话,那么操作完以后至少会出现两个次大的数字),如果有还有检查一下操作前是否有次大的数字,然后根据检查情况输出Max或Max-1;如果出现两次,检查两个最大数字周围有没有比它小2的数字,如果有输出Max,没有的话输出Max+1,(Max>=2且不满足前者,周围一定有数字非零);对于次数大于等于3次的,检查最大数字周围有没有非零数字,如果有输出Max+1,否则sum-Max。

#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<algorithm> using namespace std; //#define local
typedef long long ll;
const int maxn = 1e5+;
int p[maxn];
int main()
{
#ifdef local
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif // local
int n;
scanf("%d",&n);
ll Max = ;
int pos = -;
for(int i = ; i < n ;i++){
scanf("%d",p+i);
if(p[i]>Max){
Max = p[i];pos = i;
}
}
if(Max == ){ printf(""); return ;}
if(Max == ){
int sum = p[];
bool canMerge = false;
for(int i = ; i < n; i++){
sum += p[i];
if(p[i]&&p[i-]) { canMerge = true; break; }
}
if(canMerge) printf("");
else printf("%d",sum);
return ;
}
long long sum = ;
bool canMerge = false;
bool lessone = false; for(int i = pos; i < n; i++){
if(p[i] == Max) sum += Max;
}
if(sum == Max){
if(pos>) { if(p[pos-]<=p[pos]-)lessone = true; }
if(pos<n-) { if(p[pos+]<=p[pos]-)lessone = true; }
if(lessone) {
for(int i = ; i < n; i++){
if(p[i] == Max-) {
printf("%d",Max);
return ;
}
}
printf("%d",Max-);
}
else printf("%d",Max);
return ;
}
if(sum == Max*){ //keng
for(int i = pos; i < n; i++){
if(p[i] == Max){
if((i>&& p[i-]<= Max- )|| (i<n-&& p[i+] <= Max- ) ){
printf("%d",Max); return ;
}
}
}
printf("%d",Max+);
} for(int i = pos; i < n; i++){
if(p[i] == Max){
if((i>&& p[i-])|| (i<n-&& p[i+] ) )
{ printf("%d",Max+); return ; }
}
} printf("%I64d",sum-Max);
return ;
}

CF Gym 100637F The Pool for Lucky Ones的更多相关文章

  1. Gym 100637F F. The Pool for Lucky Ones

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  2. Gym 100637F F. The Pool for Lucky Ones 暴力

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  3. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  4. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  5. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...

  6. CF Gym 100685A Ariel

    传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  8. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

  9. CF GYM 100703B Energy Saving

    题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...

随机推荐

  1. Ubuntu如何锁定分辨率

    终于把Ubuntu的虚拟机装好了,但是分辨率没有1920*1080是什么鬼啊? 下面详细讲一下如何设置1920*1080的分辨率并设置,主要都是照着前辈的博客自己在操作一遍熟悉一下,嘿嘿. 1.安装v ...

  2. HDU 1512 Monkey King (左偏树+并查集)

    题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...

  3. hdu 6155 - Subsequence Count

    话说这题比赛时候过的好少,连题都没读TOT 先考虑dp求01串的不同子序列的个数. dp[i][j]表示用前i个字符组成的以j为结尾的01串个数. 如果第i个字符为0,则dp[i][0] = dp[i ...

  4. 洛谷P2652 同花顺

    P2652 同花顺 题目背景 所谓同花顺,就是指一些扑克牌,它们花色相同,并且数字连续. 题目描述 现在我手里有n张扑克牌,但它们可能并不能凑成同花顺.我现在想知道,最少更换其中的多少张牌,我能让这 ...

  5. Robot Frame应用实例讲解

    关键字与变量 内置库(build in)关键字 要使用的其它库关键字(学会看文档)

  6. JS数组去重精简版

    看了很多人写的好几个去重方法,我在这里精简组合下,适用于已排序与未排序的数组. 废话不多说,上代码. <!DOCTYPE html> <html> <head> & ...

  7. jQuery EasyUI/TopJUI创建树形表格下拉框

    jQuery EasyUI/TopJUI创建树形表格下拉框 第一种方法(纯HTML创建) <div class="topjui-row"> <div class= ...

  8. day5字典作业详解

    1.day5题目 1.有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2&q ...

  9. vue教程6-图表

    引入 cnpm install echarts --save #在vue项目目录下安装echarts 静态折线图 linechart.js import echarts from 'echarts' ...

  10. Avito Cool Challenge 2018-B. Farewell Party(思维)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...