cswork / OA打开练习工作台

AmazonOA #342

Server Selection

估计困难可在线判题

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

Delete some elements from an integer array; the remaining elements can be rearranged freely into a ring in which every pair of adjacent elements, including the first and last, has an absolute difference ≤1. Find the maximum number of elements kept. Equal values can be used as many times as they occur, and no elements can be added.

输入格式

The first line contains n and the second line contains n integers. On this site 1≤n≤200000, −10^9≤value≤10^9.

输出格式

Output a single integer, the answer.

样例 1

输入:

6
4 3 5 1 2 1

输出:

3

样例 2

输入:

4
1 2 2 3

输出:

4

样例 3

输入:

1
5

输出:

1

样例说明

The three answers are 3, 4 and 1. The source's first example says 4, but its explanation conjures up an extra 2; with the given array at most 3 can be kept. In the second example, 1,2,3,2 does form a ring of length 4.