cswork / OA打开练习工作台

AmazonOA #60

Get Max Servers (Circular Adjacent Diff ≤ 1)

估计困难可在线判题

考点(AI 标注)哈希计数双指针滑窗

Choose as many servers as possible and arrange them in a ring in any order so that the absolute power difference of every adjacent pair (including the first and last) is ≤1. Choosing just one server is allowed.

输入格式

The first line contains n (1..200000), and the second line contains n power values (0..10⁹).

输出格式

Output a single integer answer.

样例 1

输入:

7
4 3 5 1 2 2 1

输出:

5

样例 2

输入:

3
1 2 3

输出:

2

样例 3

输入:

2
7 7

输出:

2

样例说明

Example 1: choose 1, 1, 2, 2, 3 and arrange them as 1, 1, 2, 3, 2, for 5 servers; there is only one server with power 3, so the ring cannot cross over to 4 and come back. Example 2: each of the three values appears once, and the end values 1 and 3 differ by 2, so only 2 can be chosen. Example 3: two servers with the same value can both be chosen.