CF1157F Maximum Balanced Circle
思路
观察到答案一定是连续的一段下凸函数或者上凸函数
直接模拟找出即可
时间复杂度为\(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]&×[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&×[ansbegin-1]==1&×[ansbegin]>1)
ansnum++;
if(belong==2&×[anslast+1]==1&×[anslast]>1)
ansnum++;
printf("%d\n",ansnum);
if(belong==1){
if(times[ansbegin-1]==1&×[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&×[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的更多相关文章
- Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle
F. Maximum Balanced Circle 题目链接 题意 给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的 ...
- 【CF1157F】Maximum Balanced Circle
题目大意:给定一个长度为 N 的序列,求是否能够从序列中选出一个集合,使得这个集合按照特定的顺序排成一个环后,环上相邻的点之间的权值差的绝对值不超过 1. 题解:集合问题与序列顺序无关,因此可以先将序 ...
- 【CF1157F】Maximum Balanced Circle 求一个相邻元素之间绝对值为小于1的最大环
题目: https://codeforces.com/contest/1157/problem/F 给出一个序列 , 我们要从序列里面挑出一些数构造成一个相邻元素之间绝对值为小于1的最大环 , 挑选的 ...
- Codeforces Round #555 (Div. 3)[1157]题解
不得不说这场div3是真的出的好,算得上是从我开始打开始最有趣的一场div3.因为自己的号全都蓝了,然后就把不经常打比赛的dreagonm的号借来打这场,然后...比赛结束rank11(帮dreago ...
- 老年OIer的Python实践记—— Codeforces Round #555 (Div. 3) solution
对没错下面的代码全部是python 3(除了E的那个multiset) 题目链接:https://codeforces.com/contest/1157 A. Reachable Numbers 按位 ...
- Codeforces Round #555 (Div. 3) c2 d e f
c2:Increasing Subsequence (hard version) 那边小取那边,然后相等比较后面的长度 #include<bits/stdc++.h> using name ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- PHP根据IP判断地区名信息的示例代码
<?php header("Content-type: text/html; charset=utf-8"); function getIP(){ if (isset($_S ...
- HNOI 2012/codevs 1994:排队
题目描述 Description 某中学有n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个 ...
- vue 页面 添加背景音乐
背景音乐 添加背景音乐 并有单击事件 循环播放 <template> <div id="page"> <div style="width ...
- Java集合框架——Set接口
第三阶段 JAVA常见对象的学习 集合框架--Set接口 List集合的特点是有序的,可重复的,是不是存在这一种无序,且能保证元素唯一的集合呢?(HashSet )这就涉及到我们今天所要讲的Set集合 ...
- 对JSON.parse()中存在转义字符的解决以及js中替换函数replace()的认识
在工作中,遇到对页面数据进行转存json格式数据后存储在数据库中.然而在显示数据时遇到无法显示json中的数据,产生的bug 问题抛出: 1.首先认识下,在JSON.parse()将后台传过来的字符串 ...
- Problems to be upsolved
Donation 官方题解尚未看懂. comet oj contest15 双11特惠hard Mobitel Small Multiple 题解 为什么可以如此缩点? Candy Retributi ...
- Windows应急响应常见命令
---恢复内容开始--- 1.查看所有连接的PID netstat -ano 2.过滤特定端口 netstat -ano | findstr “443” 3.查看占用443端口的进程 tasklist ...
- 使用rsync工具构建php项目管理平台
对于phper来说部署项目和更新项目是很方便的,只要直接将写好的项目覆盖到项目的根目录就可以啦.但是平时项目开发的时候肯定不是只部署一个环境,一般是三套环境(开发环境.测试环境.生产环境),我们每次在 ...
- linux fork进程请谨慎多个进程/线程共享一个 socket连接,会出现多个进程响应串联的情况。
昨天组内同学在使用php父子进程模式的时候遇到了一个比较诡异的问题 简单说来就是:因为fork,父子进程共享了一个redis连接.然后父子进程在发送了各自的redis请求分别获取到了对方的响应体. 复 ...
- WPF 位图处理相关类
位图的存储方式 开始之前,先了解下位图的存储方式 位图的像素都分配有特定的位置和颜色值.每个像素的颜色信息由RGB组合或者灰度值表示,根据位深度,可将位图分为1.4.8.16.24及32位图像等.每个 ...