CF1157F.Maximum Balanced Circle

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn people in a row. The height of the ii-th person is aia_i. 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][a_{i_1}, a_{i_2}, \dots, a_{i_k}], where kk is the number of people you choose. Then the condition ∣aij−aij+1∣≤1|a_{i_j} - a_{i_{j + 1}}| \le 1 should be satisfied for all jj from 11 to k−1k-1 and the condition ∣ai1−aik∣≤1|a_{i_1} - a_{i_k}| \le 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.

有 nn 个人排成一列,第 ii 个人的身高为 aia_i。你可以从中任选一个子集,并尝试将所选的人排成一个平衡的环形队列。

一个平衡的环形队列是指:其中任意两个相邻人的身高之差的绝对值不超过 11。例如,设所选人员的身高序列为 [ai1,ai2,…,aik][a_{i_1}, a_{i_2}, \dots, a_{i_k}],其中 kk 为你所选的人数,则需对所有 j=1,2,…,k−1j = 1, 2, \dots, k-1 满足 ∣aij−aij+1∣≤1|a_{i_j} - a_{i_{j + 1}}| \le 1,且还需满足首尾相邻条件 ∣ai1−aik∣≤1|a_{i_1} - a_{i_k}| \le 1。此处 ∣x∣|x| 表示 xx 的绝对值。显然,仅含一人的环形队列是平衡的。

你的任务是选出尽可能多的人,并构造一个由所有被选中者组成的平衡环形队列。由于单人环形队列总是平衡的,因此答案一定存在。

输入格式

The first line of the input contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of people.

The second line of the input contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤2⋅1051 \le a_i \le 2 \cdot 10^5), where aia_i is the height of the ii-th person.

输入的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示人数。

输入的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤2⋅1051 \le a_i \le 2 \cdot 10^5),其中 aia_i 表示第 ii 个人的身高。

输出格式

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,…,reskres_1, res_2, \dots, res_k, where resjres_j is the height of the jj-th person in the maximum balanced circle. The condition ∣resj−resj+1∣≤1|res_{j} - res_{j + 1}| \le 1 should be satisfied for all jj from 11 to k−1k-1 and the condition ∣res1−resk∣≤1|res_{1} - res_{k}| \le 1 should be also satisfied.

在输出的第一行打印 kk —— 最大平衡圆圈中的人数。

在第二行打印 kk 个整数 res1,res2,…,reskres_1, res_2, \dots, res_k,其中 resjres_j 表示最大平衡圆圈中第 jj 个人的身高。需满足对所有 j=1j = 1 到 k−1k-1,有 ∣resj−resj+1∣≤1|res_{j} - res_{j + 1}| \le 1,且还需满足 ∣res1−resk∣≤1|res_{1} - res_{k}| \le 1。

输入输出样例

  • 输入#1

    7
    4 3 5 1 2 2 1

    输出#1

    5
    2 1 1 2 3
  • 输入#2

    5
    3 7 5 1 5

    输出#2

    2
    5 5
  • 输入#3

    3
    5 1 4

    输出#3

    2
    4 5
  • 输入#4

    7
    2 2 3 2 1 2 2

    输出#4

    7
    1 2 2 2 2 3 2

输入解题思路,AI测评打分。不知道怎么写?

首页