CF Gym 100637F The Pool for Lucky Ones
题意:给你一串非负整数,可以将一个非零数减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的更多相关文章
- 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 ...
- 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 ...
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
- CF gym 101933 K King's Colors —— 二项式反演
题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...
- 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 ...
- CF Gym 100685A Ariel
传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
- CF GYM 100703A Tea-drinking
题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...
- CF GYM 100703B Energy Saving
题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...
随机推荐
- Open-source Tutorial - NLog
1. Installing NLog 使用 NuGet 程序包管理器安装 NLog.如何使用 NuGet? 遇到问题:我的项目是 .Net Framework 4.0 平台的,虽然 NLog 说明中是 ...
- Unite 2017 干货整理 同步篇
http://www.kisence.com/2017/05/17/unite-2017-gan-huo-zheng-li-tong-bu-pian/ Unite 2017 干货整理 同步篇 2017 ...
- [Swift]Xcode实际操作
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Node.js 内置模块crypto加密模块(2) AES
AES:高级加密标准 ( Advanced Encryption Standard ) AES是一种对称加密算法:加密需要密钥,且加密密钥和解密密钥相同 下面是AES加密的Node实现: " ...
- Php5.6.15-fpm的运行机制源码剖析
源码版本:Php5.6.15 源码目录:sapi/fpm/fpm 说明:源码的主要功能在上面直接注解 =============>>start<<=============== ...
- Python小世界:匿名函数、高阶函数、推导式
前言 木子本人搞起Python已有多年,一直觉得什么都会,但是有时候实操起来,才觉得很多底层基础的知识都没有彻底的灵活掌握. 另外,网上关于Python基础知识的各种普及已有太多太多. 附上相关大神的 ...
- Codeforces Round #564 (Div. 2) A. Nauuo and Votes
链接:https://codeforces.com/contest/1173/problem/A 题意: Nauuo is a girl who loves writing comments. One ...
- php:比较两个txt文件,格式如下,分别取出a.txt有的b.txt没有的,b.txt有的a.txt没有的及两个都有的
<?php /*比较两个txt文件,格式如下,分别取出a.txt有的b.txt没有的,b.txt有的a.txt没有的及两个都有的 * a.txt: * A * B * C * D * b.txt ...
- IIS中的 Asp.Net Core 和 dotnet watch
在基于传统的.NET Framework的Asp.Net Mvc的时候,本地开发环境中可以在IIS中建立一个站点,可以直接把站点的目录指向asp.net mvc的项目的根目录.然后build一下就可以 ...
- F. Clique in the Divisibility Graph DP
http://codeforces.com/contest/566/problem/F F. Clique in the Divisibility Graph time limit per test ...