题意:给你一串非负整数,可以将一个非零数减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. SO_REUSEADDR的分析

    今天协议个socket程序时碰到了这个问题,选自博客http://www.cppblog.com/ace/archive/2006/04/29/6446.html 敲完代码,等下看它.

  2. conntrack-tools使用

    基础用法 系统配置 ### 开启流数据包统计(packets和bytes) # echo "net.netfilter.nf_conntrack_acct=1" >> ...

  3. SCUT - 15 - 为美好的世界献上爆炎 - dfs

    https://scut.online/p/15 样例错了,按题目说的去做就AC了. 反向搜索使得最终比较strncmp的时候复杂度下降了很多(虽然应该可行性剪枝也少了) #include<bi ...

  4. php 生成图片,只获取字节数据,不以图片格式输出

    提示:与直接将结果输出到浏览器的任何内容一样,可以使用输出控制函数(http://www.php.net/./en/ref.outcontrol.php)捕获此函数的输出,并将其保存在字符串(例如)中 ...

  5. Spfa算法模板

    输入点数n,边数m,起点终点边权 输出1号节点到所有点的最短路径长度 #include<iostream> #include<queue> #include<cstrin ...

  6. Ruby: Case表达式

    Ruby的case表达式有两种形式: 第一种形式接近于一组连续的if语句:它让你列出一组条件,并执行第一个为真的条件表达式所对应的语句. 第二种形式,在case语句的顶部指定一个目标,而每个when从 ...

  7. jQuery EasyUI/TopJUI上传多个附件并可以进行删除操作

    jQuery EasyUI/TopJUI上传多个附件并可以进行删除操作 html <table data-toggle="topjui-datagrid" data-opti ...

  8. 题解 [HNOI2002]DNA分子的最佳比对 (洛谷P2268)

    题目传送门 (这道题告诉我:初始化要小心) 有没有一点最长公共子序列的赶脚(口胡) 但我觉得挺像 设 表示匹配到s1的第i位,s2的第j位,则有 的初始化要注意:根据的含义,表示s1[i]一直在匹配空 ...

  9. NET Core 2.1 Global Tools

    微软工程师Nate McMaster的博文.NET Core 2.1 Global Tools https://natemcmaster.com/blog/2018/05/12/dotnet-glob ...

  10. NET Core 2.0 使用支付宝

    ASP.NET Core 2.0 使用支付宝PC网站支付   前言 最近在使用ASP.NET Core来进行开发,刚好有个接入支付宝支付的需求,百度了一下没找到相关的资料,看了官方的SDK以及Demo ...