思路

观察到答案一定是连续的一段下凸函数或者上凸函数

直接模拟找出即可

时间复杂度为\(O(n)\)

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,a[200200],times[200200],minval=0x3f3f3f3f,maxval=0,ansbegin,anslast,ansnum,beginx,last,num,belong;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
minval=min(minval,a[i]);
maxval=max(maxval,a[i]);
}
for(int i=1;i<=n;i++)
times[a[i]]++;
for(int i=minval;i<=maxval;i++)
if(times[i]&&times[i-1])
ansnum=2,ansbegin=i-1,anslast=i,belong=1;
for(int i=minval-1;i<=maxval+1;i++){
if(times[i]>1){
num+=times[i];
last=i;
if(num>ansnum){
belong=1;
ansnum=num;
ansbegin=beginx;
anslast=last;
}
}
else if(times[i]==1){
num+=times[i];
last=i;
if(num>ansnum){
belong=1;
ansnum=num;
ansbegin=beginx;
anslast=last;
}
num=0;
beginx=i+1;
}
else{
num=0;
beginx=i+1;
}
}//上凸 for(int i=maxval+1;i>=minval-1;i--){
if(times[i]>1){
num+=times[i];
beginx=i;
if(num>ansnum){
belong=2;
ansnum=num;
ansbegin=beginx;
anslast=last;
}
}
else if(times[i]==1){
num+=times[i];
beginx=i;
if(num>ansnum){
belong=2;
ansnum=num;
ansbegin=beginx;
anslast=last;
}
num=0;
last=i-1;
}
else{
num=0;
last=i-1;
}
}// 下凸
if(belong==1&&times[ansbegin-1]==1&&times[ansbegin]>1)
ansnum++;
if(belong==2&&times[anslast+1]==1&&times[anslast]>1)
ansnum++;
printf("%d\n",ansnum);
if(belong==1){
if(times[ansbegin-1]==1&&times[ansbegin]>1)
printf("%d ",ansbegin-1);
for(int i=ansbegin;i<=anslast;i++){
while(times[i]>1){
printf("%d ",i);
times[i]--;
}
}
for(int i=anslast;i>=ansbegin;i--){
if(times[i])
printf("%d ",i);
}
}
else{
if(times[anslast+1]==1&&times[anslast]>1)
printf("%d ",anslast);
for(int i=anslast;i>=ansbegin;i--){
while(times[i]>1){
printf("%d ",i);
times[i]--;
}
}
for(int i=ansbegin;i<=anslast;i++){
if(times[i])
printf("%d ",i);
}
}
return 0;
}

CF1157F Maximum Balanced Circle的更多相关文章

  1. Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle

    F. Maximum Balanced Circle 题目链接 题意 给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的 ...

  2. 【CF1157F】Maximum Balanced Circle

    题目大意:给定一个长度为 N 的序列,求是否能够从序列中选出一个集合,使得这个集合按照特定的顺序排成一个环后,环上相邻的点之间的权值差的绝对值不超过 1. 题解:集合问题与序列顺序无关,因此可以先将序 ...

  3. 【CF1157F】Maximum Balanced Circle 求一个相邻元素之间绝对值为小于1的最大环

    题目: https://codeforces.com/contest/1157/problem/F 给出一个序列 , 我们要从序列里面挑出一些数构造成一个相邻元素之间绝对值为小于1的最大环 , 挑选的 ...

  4. Codeforces Round #555 (Div. 3)[1157]题解

    不得不说这场div3是真的出的好,算得上是从我开始打开始最有趣的一场div3.因为自己的号全都蓝了,然后就把不经常打比赛的dreagonm的号借来打这场,然后...比赛结束rank11(帮dreago ...

  5. 老年OIer的Python实践记—— Codeforces Round #555 (Div. 3) solution

    对没错下面的代码全部是python 3(除了E的那个multiset) 题目链接:https://codeforces.com/contest/1157 A. Reachable Numbers 按位 ...

  6. Codeforces Round #555 (Div. 3) c2 d e f

    c2:Increasing Subsequence (hard version) 那边小取那边,然后相等比较后面的长度 #include<bits/stdc++.h> using name ...

  7. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  8. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  9. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. Python安装pip3

    在新的电脑上,偶尔会出现无法安装pip的问题. 今天看见了一位大神的方法: 在cmd窗口中输入python -m ensurepip 然后使用pip命令安装其他包.pip3 install 模块名 说 ...

  2. Mysql批量插入返回Id错乱(原因分析)

    在项目中经常会有如下场景: 往数据库中批量插入一批数据后,需要知道哪些插入成功,哪些插入失败了. 这时候往往会有两种思路,一个是在插入之前判断相同的记录是否存在,过滤掉重复的数据:另外一种就是边插入边 ...

  3. kafka的offset相关知识

    Offset存储模型 由于一个partition只能固定的交给一个消费者组中的一个消费者消费,因此Kafka保存offset时并不直接为每个消费者保存,而是以 groupid-topic-partit ...

  4. 【leetcode算法-简单】13. 罗马数字转整数

    [题目描述] 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列 ...

  5. PTA(Advanced Level)1048.Find Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  6. idea npm vue java开发工具安装 环境配置

    感谢此链接内容作者,从前往后流程较完整详细,助我成功配置好(不知道在这之前做的一些尝试有没有影响) https://blog.csdn.net/qq_42564846/article/details/ ...

  7. springboot service dockerfile

    FROM java:8u111 MAINTAINER ianthony7@163.com # 定义变量 ENV WORK_DIR /opt ENV LOG_DIR /data/logs EXPOSE ...

  8. 计算机基础与python安装

    计算机基础 内容详细: 一.计算机基础 1. 计算机什么组成的 输入输出设备 cpu 硬盘 内存 中央处理器 处理各种数据 相当于人的大脑 内存 存储数据 硬盘 存储数据的 2. 什么是操作系统 控制 ...

  9. matplotlib 画图中的basemap安装问题

    在我用matplotlib画图是会用到,basemap这个库,但是直接安装却会出现问题: 使用这条安装命令就能够安装功能了: conda install -c conda-forge basemap= ...

  10. npm无法安装node-sass的解决方法

    使用npm install 命令安装node-sass时,经常出现安装失败的情况.原因在于npm服务器在美国,还有就是某强大的防火墙作用.导致模块无法下载. npm install node-sass ...