Contest1391 - 2018年第三阶段个人训练赛第六场. Together(水题)

问题 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;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值