题目描述
You are given an integer sequence of length N, a1,a2,…,aN.
For each 1≤i≤N, you have three choices: add 1 to ai, subtract 1 from ai or do nothing.
After these operations, you select an integer X and count the number of i such that ai=X.
Maximize this count by making optimal choices.
Constraints
1≤N≤105
0≤ai<105(1≤i≤N)
ai is an integer.
输入
The input is given from Standard Input in the following format:
N
a1 a2 .. aN
输出
Print the maximum possible number of i such that ai=X.
样例输入
7 3 1 4 1 5 9 2
样例输出
4
提示
For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.
这道题不涉及什么算法,直接看代码吧
#include<iostream>
using namespace std;
const int N=1e5+5;
int a[N];
int main(){
int n;
cin>>n;
int ai;
while(n--){
scanf("%d",&ai);
a[ai]++; //把读取到的数的前后数都加1,最后那个数的值最大,答案就是多少
a[ai-1]++;
a[ai+1]++;
}
int max=0;
for(int i=0;i<N-1;i++){
if(max<a[i])
max=a[i];
}
cout<<max<<endl;
return 0;
}