Together(AtCoder-3524)

Problem Description

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≤iN)
  • ai is an integer.

Input

The input is given from Standard Input in the following format:

N
a1 a2 .. aN

Output

Print the maximum possible number of i such that ai=X.

Example

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

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.

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1

题意:给一个长度为 n 的整数序列,对于序列中的每一个数可以进行一次操作,可以对其 +1、-1 或不做任何操作,对这 n 个数进行一次操作后,问序列中次数出现最多的数

思路:由于给出的数 ai 的值不大,可以利用桶排,对于每个数将其 +1、-1、原值均存入桶中,最后扫一遍桶,桶中元素最大值就是答案,需要注意的是,由于 ai 最小可能为 0,将其 -1 后值为 -1,因此桶需要整体向右偏移一个

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int bucket[N];
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        x++;//向右偏移
        bucket[x]++;
        bucket[x-1]++;
        bucket[x+1]++;
    }

    int maxx=-INF;
    int res=0;
    for(int i=1;i<=1E5+1;i++)
        maxx=max(maxx,bucket[i]);
    
    printf("%d\n",maxx);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值