QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#687615#6433. Klee in Solitary ConfinementKopicyTL 0ms0kbC++231.3kb2024-10-29 20:00:232024-10-29 20:00:23

Judging History

你现在查看的是最新测评结果

  • [2024-10-29 20:00:23]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-29 20:00:23]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
//#define int long long
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define sz(x) (int)x.size()
#define endl "\n"
const int N = 2e5, mod = 1e9 + 7, inf = 1e18;

int qpow(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}

void solve() {
    int n,k;
    cin>>n>>k;
    map<int,vector<int>> pos;

    for(int i=1;i<=n;i++){
        int x;cin>>x;
        pos[x].push_back(i);
    }
    int ans=0;
    for(auto &[x,vec]:pos){
        ans=max(ans,sz(vec));
    }
    if(!k){
        cout<<ans;
        return;
    }
    for(auto &[x,vec]:pos){
        int y=x+k;
        vector<array<int,2>> tmp;
        for(auto i:pos[x]) tmp.push_back({i,1});
        for(auto i:pos[y]) tmp.push_back({i,-1});

        sort(tmp.begin(),tmp.end(),[&](auto aa,auto bb){
            return aa[0]<bb[0];
        });
        int p=0,ans1=0;
        for(int i=0;i<sz(tmp);i++){
            ans1=max(p+tmp[i][1],ans1);
            p=max(p+tmp[i][1],0);
        }
        ans=max(ans,ans1+sz(pos[y]));
    }
    cout<<ans;
}

signed main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tt = 1;
    //cin >> tt;
    rep(kase, 1, tt) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

5 2
2 2 4 4 4

output:


result: