B题求调,只能通过42%

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[500010];
int per[500010];
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,m,k;
    cin>>n>>m>>k;
    int i;
    map<int,int>d;
    int cnt=0;
    for(i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(d[x]==1)continue;
        d[x]=1;
        a[++cnt]=x;
    }
    sort(a+1,a+1+cnt);
    map<int,int>id;
    for(i=1;i<=cnt;i++)
    {
        if(a[i]-a[i-1]>1)
        per[i]=per[i-1]+a[i]-a[i-1]-1;
        else
        per[i]=per[i-1];
        if(id[per[i]]==0)
        id[per[i]]=i;
    }
    int maxn=0;
    for(i=1;i<=cnt;i++)
    {
        if(id[per[i]-k]==0)
        {
            maxn=max(maxn,i-id[per[i]]+1+k);
        }
        else
        maxn=max(maxn,i-id[per[i]-k]+1+k);
    }
    if(per[cnt]<k)
    {
        int d=k-per[cnt];
        cout<<min(n+k,min(m,n+d));
    }
    else
    cout<<maxn;
    return 0;
}

全部评论
没去重
点赞 回复 分享
发布于 2024-12-02 20:12 河北
Hack数据: ``` 1 8 12 8 ``` 答案:8 你的输出:6 @悲伤与快乐
点赞 回复 分享
发布于 2024-11-30 09:27 广东
wk一模一样的思路,我也没过
点赞 回复 分享
发布于 2024-11-30 09:17 辽宁

相关推荐

08-22 10:08
中南大学 营销
点赞 评论 收藏
分享
07-07 17:06
已编辑
深圳技术大学 golang
懒羊羊王子:能不能看一下你的简历,我想参考一下我应该学到什么程度,在面试期间你又是如何应对问题的
点赞 评论 收藏
分享
08-01 11:19
电气工程师
我懒羊羊觉得没问题:写的太学生化了,像作文一样,很难看出你和岗位的匹配度
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务