题意:给你一串非负整数,可以将一个非零数减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. Software - (转)Winform 程序捕获全局异常

    static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...

  2. Unity中场景异步加载

    引入命名空间 using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; using Syst ...

  3. PHP之递归函数

    https://www.cnsecer.com/4146.html http://www.jb51.net/article/71424.htm //一列数字的规则如下:1,1,2,3,5,8,13,2 ...

  4. FZU2216【二分】

    题意: 百度. 思路: 一个连续数组111222233344444555666的每一个起伏转折即需要一张万能牌. 然后二分一下得最长区间. #include<cstdio> #includ ...

  5. Python学习笔记(异常处理)

    用户输入了一个不合规定的值,或者需要打开的文件不存在.这些情况被称作“异常”,一个好的程序需要能处理可能发生的异常,避免程序因此而中断. 例如我们去打开一个文件: f = file('non-exis ...

  6. loadrunner教程系列

    loadrunner教程系列,包括windows 环境和linux 环境. 第一讲:环境准备 链接:https://pan.baidu.com/s/1EnLP3ijZ1j1I_ysE1z4CJg  密 ...

  7. JQuery Easyui/TopJUI 用JS创建数据表格并实现增删改查功能

    JQuery Easyui/TopJUI 用JS创建数据表格并实现增删改查功能 html <table id="productDg"></table> &l ...

  8. [Android基础]Android四大组件之Activity总结

    1.Activity简介 Activity是Android一个非常重要的用户接口(四大组件之一),是可见的,主要是用户和应用程序之间进行交互的接口.在每个Activity中都可以放很多控件,所以也可以 ...

  9. 题解 poj1845 Sumdiv (数论) (分治)

    传送门 大意:求A^B的所有因子之和,并对其取模 9901再输出 (这题又调了半天,把n和项数弄混了QAQ) 根据算数基本定理:A=(p1^k1)*(p2^k2)*(p3^k3)*...*(pn^kn ...

  10. B.Icebound and Sequence

    链接:https://ac.nowcoder.com/acm/contest/903/B 题意: Icebound hates math. But Imp loves math. One day, I ...