[构造] F. Maximum Balanced Circle

There are nn people in a row. The height of the ii-th person is aiai. You can choose any subset of these people and try to arrange them into a balanced circle.

A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than 11. For example, let heights of chosen people be [ai1,ai2,…,aik][ai1,ai2,…,aik], where kk is the number of people you choose. Then the condition |aij−aij+1|≤1|aij−aij+1|≤1should be satisfied for all jj from 11 to k−1k−1 and the condition |ai1−aik|≤1|ai1−aik|≤1 should be also satisfied. |x||x| means the absolute value of xx. It is obvious that the circle consisting of one person is balanced.

Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of people.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the height of the ii-th person.

Output

In the first line of the output print kk — the number of people in the maximum balanced circle.

In the second line print kk integers res1,res2,…,reskres1,res2,…,resk, where resjresj is the height of the jj-th person in the maximum balanced circle. The condition |resj−resj+1|≤1|resj−resj+1|≤1 should be satisfied for all jj from 11 to k−1k−1 and the condition |res1−resk|≤1|res1−resk|≤1 should be also satisfied.

题意:

找出一个环, 使得相邻元素最大差值为1,且环最长

思路: bucket计数

尺取记录符合条件的最大区间, 左一右一中间多即可

代码:

/*
    Zeolim - An AC a day keeps the bug away
*/

//pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <sstream>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <long long, null_type, less <long long>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 10;
const ll MOD = 1e9 + 7;

int cnt[MAXN] = {0};

int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);     cout.tie(0);
    //freopen("D://test.in", "r", stdin);
    //freopen("D://test.out", "w", stdout);
	
	int n, ma = 0;

	cin >> n;

	for(int i = 0, x; i < n; ++i)
	{
		cin >> x;
		++cnt[x];
		x > ma ? ma = x : x;
	}

	int ans = 0, pa = 0, pb = 0, sum = 0;

	for(int i = 1, j; i <= ma; ++i)
	{
		sum = 0;
		
		if(cnt[i])
		{
			if(cnt[i] == 1)
				sum = 1;
				
			else sum = cnt[i];
			
			j = i;
			
			while(cnt[j + 1] >= 2) sum += cnt[j + 1], ++j;
			
			if(cnt[j + 1]) ++sum, ++j;
			
			if(sum > ans)
			{
				ans = sum, pa = i, pb = j;
			}
			
			if(j != i)
				i = j - 1;
		}
	}
	
	deque <int> DQ;

	for(int i = pb; i >= pa; i--)
	{
		while(cnt[i])
		{
			if(cnt[i] & 1)
				DQ.push_back(i);
			else
				DQ.push_front(i);
			--cnt[i];
		}
	}


	cout << ans << '\n';

	for(auto x : DQ)
		cout << x << ' ';
		
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值