F. Maximum Balanced Circle

题目链接

题意

给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的数相差不超过1。

最后要求输出具体的方案。

题解

一开始想了一个dp,似乎也可以做

这个题也不用这么复杂,因为相差绝对值不超过1,直接统计一下每个数的个数就行了。

因为如果将最后的环给展开,以每个数的值为高,呈现出来的图形一定是先上升后下降的。那么中间部分的数的个数一定大于等于2,最左边和最右边的两个数特殊考虑一下就行了。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n;
int a[N], c[N];
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]), c[a[i]]++;
int ans = 0;
int l, r;
int ansl, ansr;
int sum = 0;
for(int i = 1; i < N; i++) {
if(c[i] >= 1 && sum == 0) {
l = i;
sum += c[i] ;
}else if(c[i] > 1) {
sum += c[i];
}else if(sum > 0){
if(c[i] == 1) sum++, r = i;
else r = i - 1;
if(sum > ans) {
ans = sum ;
ansl = l;
ansr = r;
}
sum = 0;
if(c[i] == 1) {
sum++;
l = i;
}
}
}
cout << ans << '\n' ;
if(c[ansl] == 1) cout << ansl << ' ', ansl++ ;
for(int i = ansl; i <= ansr; i++) {
for(int j = 1; j < c[i]; j++) {
printf("%d ",i) ;
}
}
for(int i = ansr; i >= ansl; i--) {
printf("%d ",i);
}
return 0;
}

Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle的更多相关文章

  1. Codeforces Round #627 (Div. 3) F - Maximum White Subtree(深度优先搜索)

    题意: n 个点 n - 1 条边的树,问每个点所在所有子树中白黑点数目的最大差. 思路: 白点先由下至上汇集,后由上至下分并. #include <bits/stdc++.h> usin ...

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

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

  3. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  4. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  5. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  6. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

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

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

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

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

  9. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

随机推荐

  1. Mac下 homebrew 安装mysql

    操作步骤 安装homebrew brew install mysql 安装mysql 安装完成之后,可以运行命令启动mysql服务 mysql.server start 然后输入命令设置密码 mysq ...

  2. jQuery.js引入时要在其他js文件之前,否则js中无法识别jQuery的语法

  3. spring mvc 处理pojo传递对象时该对象继承父类的属性在网络接收端接收该属性值总是null,why?

    //=========================== 情形一: ===============================//在网络上传递User1类对象时info属性值在网络的另一端能够接 ...

  4. SGU 128. Snake --- 暴力枚举+并查集+贪心+计算几何

    <传送门> 128. Snake time limit per test: 0.25 sec. memory limit per test: 4096 KB There are N poi ...

  5. laravel 自定义验证 Validator::extend

    laravel 自定义验证 $messages = [ 'name.integer' => '名字不能为整型', 'name.max' => '长度不能超过5', ]; public st ...

  6. emmet 配置文件

    snippets.json(添加自己的或更新现有的片段) preferences.json(更改某些Emmet过滤器和操作的行为) SyntaxProfiles.json(定义生成的HTML / XM ...

  7. 论DOM中文档和元素的位置大小属性及其区别

    element.offsetLeft/Top  获取元素相对于最近的有定位的父元素的坐标,如果没有有定位的父元素,则是文档坐标 element.scrollTop/Left 获取元素滚动卷去的距离 e ...

  8. hystrix简介

    hystrix,框架,提供了高可用相关的各种各样的功能,然后确保说在hystrix的保护下,整个系统可以长期处于高可用的状态,100%. 高可用系统架构: 资源隔离.限流.熔断.降级.运维监控 资源隔 ...

  9. SpringBoot的入门程序

    1. 创建一个springboot工程 可以参考springboot入门程序 2. 创建一个实体类 @Data //想相当于@Setter.@Getter和@ToString替代了setter.get ...

  10. django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法

    django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法 django.db ...