Codeforces Round #555 (Div. 3)F. Maximum Balanced Circle

F. Maximum Balanced Circle

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

Copy

7
4 3 5 1 2 2 1

output

Copy

5
2 1 1 2 3

input

Copy

5
3 7 5 1 5

output

Copy

2
5 5 

input

Copy

3
5 1 4

output

Copy

2
4 5 

input

Copy

7
2 2 3 2 1 2 2

output

Copy

7
1 2 2 2 2 3 2 

 

 

分析:要构成一个圆,那么除了最小的元素和最大的元素,其他元素至少需要2个,那么排序后直接找连续的子串,并且在遇到只出现一次的元素时check一下,记录最大值。输出的时候用multiset维护一下即可。

 

#include<bits/stdc++.h>

using namespace std;
int a[200004];

int main() {
    int n;
    cin >> n;
    unordered_map<int, int> mp;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        mp[a[i]]++;
    }
    sort(a + 1, a + 1 + n);
    int ansl = 1, ansr = 1, len = 1;
    int l = 1, r = 1;
    for (int i = 2; i <= n; ++i) {
        if (abs(a[i] - a[i - 1]) <= 1 && mp[a[i]] > 1)r = i;
        else {
            if (abs(a[i] - a[i - 1]) <= 1 && mp[a[i]] == 1)r = i;
            if (len < r - l + 1) {
                len = r - l + 1;
                ansl = l;
                ansr = r;
            }
            l = i;
        }
    }
    if (len < r - l + 1) {
        len = r - l + 1;
        ansl = l;
        ansr = r;
    }
    printf("%d\n", len);
    multiset<int> s;
    for (int i = ansl; i <= ansr; ++i) {
        s.insert(a[i]);
    }
    int las = 0;
    while (s.size()) {
        auto pos = s.upper_bound(las);
        if (pos == s.end())break;
        else printf("%d ", *pos);
        las = *pos;
        s.erase(pos);
    }
    while (s.size()) {
        auto pos = --s.end();
        printf("%d ", *pos);
        s.erase(pos);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值