# Untitled

unknown
c_cpp
a year ago
2.7 kB
3
Indexable
Never
```#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <deque>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <iomanip>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using db = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<db, db>;
using tiii = tuple<int, int, int>;
using str = string;

#define vt vector
#define pb push_back
#define eb emplace_back
#define ins insert
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

void solve() {
int n, k;
cin >> n >> k;
vt<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vt<pii> q(n);
for (int i = 0; i < n; ++i) {
q[i].second = -1;
}
vt<vt<int>> d(n);
for (int i = 0; i < n; ++i) {
if (q[a[i] - 1].second == -1)
q[a[i] - 1].second = i;

else {
d[a[i] - 1].pb(i - 1 - q[a[i] - 1].second);
q[a[i] - 1].se = i;
}
}

// for (auto s : d) {

//     for (int i : s) {
//         cout << i << ' ';
//     }

//     cout << '\n';
// }

int answ = 0;

for (auto s : d) {

if (sz(s) > 0) {
vt<int> p(n);
p[0] = s[0];

for (int i = 1; i < sz(s); ++i) {
p[i] = p[i - 1] + s[i];
}

int cur = 0;
for (int i = 0; i < sz(s); ++i) {
int l = -1;
int r = sz(s);

while (r > l + 1) {
int m = (l + r) / 2;
if (p[m] <= p[i] + k) {
l = m;
} else {
r = m;
}
}
cur = max(cur, l - i + 1);

// cout << i << ' ' << cur << '\n';
}

answ = max(answ, cur);
}
}

cout << answ + 1 << endl;
}

signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
//     freopen("data.in", "r", stdin);
//     freopen("data.out", "w", stdout);
int tests = 1;
//    cin >> tests;
while (tests--) {
solve();
}
return 0;
}
```