哈尔滨理工大学软件与微电子学院程序设计竞赛L-Defeat the monster

Defeat the monster

https://ac.nowcoder.com/acm/contest/5929/L

题解:
我们可以先把他按照能力值水平由小到大进行排序,在进行双指针操作,我们每次都让右指针每次都移动,如果相差大于5的话我们就把左指针往右移动一下,并且如果(右指针指针指向的数-左指针指向的数)小于5的话,我们需要更新一下之间的差值。

/*Keep on going Never give up*/
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
const int maxn = 2e5+10;
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
typedef long long ll;
const int mod = 100000000;
using namespace std;

int a[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    sort(a,a+n);
    int ans=0,l=0;
    for(int i=0;i<n;i++){
        if(a[i]-a[l]<=5){
            ans=max(ans,i-l+1);
        }
        else l++;
    }
    cout<<ans<<endl;
    return 0;
}
题解 文章被收录于专栏

主要写一些题目的题解

全部评论

相关推荐

群星之怒:不是哥们,你就不好奇瘫痪三十年的老植物人是啥样的吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务