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 ...
随机推荐
- js 获取xxxx-xx-xx时间格式
function getdate() { var now = new Date(), y = now.getFullYear(), m = now.getMonth() + 1, d = now.ge ...
- [OpenCV] sift demo
运行环境:vs2012+opencv320 sift 需要的头文件为 <opencv2/xfeatures2d.hpp> #include <opencv2/opencv.hpp&g ...
- 使用nginx构建限频、限速、限并发的应用保护层
使用nginx构建限频.限速.限并发的应用保护层 nginx本身提供了基础的限频.限速.限并发连接等能力. 限频 基于uri等限制某一个客户端,某类客户端持续时间段内建立连接的次数. 限速 限制客户端 ...
- bat命令教程
转自:https://www.jb51.net/article/151923.htm 第一章 批处理基础 第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合 ...
- [转帖]Linux systemd 常用命令
Linux systemd 常用命令 https://www.cnblogs.com/tsdxdx/p/7288490.html systemctl hostnamectl timedatectl l ...
- 小菜鸟之oracle触发器
1.触发器说明 触发器是一种在事件发生时隐式地自动执行的PL/SQL块,不能接受参数,不能被显式调用 2.触发器类型 根据触发器所创建的语句及所影响的对象的不同,将触发器分为以下3类 (1)DML触发 ...
- 【AtCoder】AGC003
AGC编号越小越水???? AGC003 A - Wanna go back home 相对方向要么一起有要么一起没有 #include <bits/stdc++.h> #define f ...
- 我学会了正确的dinic
以前写Isap的时候,总是被卡,然后学了一发Isap的当前弧优化,好像可以水过很多题 但是一直没明白为啥Isap会走一个环??? 然后写dinic了,听说不容易被卡(来自去年九省联考的指导) 然而-- ...
- HTTP用户认证、追加协议以及相关技术简单学习
1. 用户身份认证 BASIC认证(基本认证): DIGEST(摘要认证): SSL客户端认证: FormBase认证(表单认证)常用: session和cookie 2. 基于HTTP的追加协议 A ...
- MySql设计表中的create_time和update_time字段
一般create_time和update_time字段的类型为datetime类型,长度为0