思路

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

直接模拟找出即可

时间复杂度为\(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. Django模板渲染之自定义inclusion_tag详细使用

    inclusion_tag在使用的时候可以帮我们减少很多前端和后端重复的代码 逻辑图: inclusion_tag的作用是主页面以一定的语法给一个参数,调用某个函数,这个函数可以通过主页面给的参数做一 ...

  2. centos6.5安装python3及virtualenv环境

    1. 下载源码: wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz wget http://mirrors.sohu.com/ ...

  3. Python list,tuple,dict,set高级变量常用方法

    list列表 增加 append 在列表中追加,一次只能加一个 insert 按索引插入,一次只能插一个 extend 迭代追加到列表中 list1 = [1, 2, 3] list2 = [4, 5 ...

  4. Python多线程VS多进程

  5. python — 函数基础知识(二)

    目录 1 返回值 2 作用域 3 函数小高级 4 函数中高级 1 返回值 def func(arg): # .... return 9 # 返回值为9 默认:return None val = fun ...

  6. gitlab安装指南(gitlab-ce-9.4.3-ce.0.el7.x86_64 centos7)

    1,安装gitlab wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-9.4.3-ce.0.el7. ...

  7. VIM纵向编辑【转】

    原文:https://www.ibm.com/developerworks/cn/linux/l-cn-vimcolumn/index.html Vim 的纵向编辑模式启动方便,使用灵活,还可以配合上 ...

  8. IntelliJ IDEA 统一设置编码为utf-8编码

    问题一: File->Settings->Editor->File Encodings 问题二: File->Other Settings->Default Settin ...

  9. C#学习资料

    http://www.runoob.com/csharp/csharp-delegate.html

  10. [转载]java中import作用详解

    [转载]java中import作用详解 来源: https://blog.csdn.net/qq_25665807/article/details/74747868 这篇博客讲的真的很清楚,这个作者很 ...