问题 K: Together
时间限制: 1 Sec 内存限制: 128 MB
提交: 371 解决: 197
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define M 3*100005
using namespace std;
struct node
{
int e;
int cnt;
};
int main()
{
int n,e;
cin>>n;
int res[M];
memset(res,0,sizeof(res));
int Max=-1;
for(int i=0;i<n;i++)
{
cin>>e;
res[e+1]++;
res[e-1]++;
res[e]++;
}
for(int i=0;i<M;i++)
Max=max(Max,res[i]);
printf("%d\n",Max);
return 0;
}