问题 K: Together
时间限制: 1 Sec 内存限制: 128 MB
提交: 360 解决: 193
[提交] [状态] [讨论版] [命题人: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.
解题思路:
由于 ai 和 n 的范围都很小,我们直接暴力求解就可以。
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(~scanf("%d", &n))
{
int b[100010],c[100010],m = -1;
memset(c, 0, sizeof(c));
for(int i=1; i<=n; i++)
{
cin>>b[i]; c[b[i]] ++;
m = max(m, b[i]);
}
int ans = 0;
for(int i=0; i<=m; i++)
ans = max(ans, c[i]+c[i+1]+c[i+2]);
cout << ans << endl;
}
return 0;
}