传送门
题意
分析
李巨丢给我的题,因为读错了题一直没思路
首先这个区间的值定义为这个区间众数出现的次数,我们都知道众数出现的次数具有一定的单调性,也就是说我们如果确定了一个区间,然后将这个区间的左右端点向两边延伸,这个区间的值只有可能增大,不会减少
所以,我们可以考虑二分这个答案,枚举一个答案,然后
c
h
e
c
k
check
check答案的合法性,怎么去
c
h
e
c
k
check
check呢,我们用尺取,先确定一个大于
m
i
d
mid
mid的区间
[
l
,
r
]
[l,r]
[l,r],那么,区间
[
l
,
r
+
1
]
,
[
l
,
r
+
2
]
.
.
.
[
l
,
n
]
[l,r + 1],[l,r + 2] ... [l,n]
[l,r+1],[l,r+2]...[l,n]都属于合法区间,然后移动左端点,做尺取即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int n;
ll k;
ll a[N];
vector<ll> nums;
int cnt[N];
int find(ll x) {
return lower_bound(nums.begin(), nums.end(), x) - nums.begin();
}
bool check(int mid){
for(int i = 0;i < SZ(nums);i++) cnt[i] = 0;
int l = 1;
ll res = 0;
for(int i = 1;i <= n;i++){
int x = find(a[i]);
cnt[x]++;
while(cnt[x] >= mid){
res += (1ll * n - i + 1);
cnt[find(a[l])]--;
l++;
}
if(res >= k) return true;
}
return false;
}
int main() {
read(n),read(k);
for(int i = 1;i <= n;i++) {
read(a[i]);
nums.pb(a[i]);
}
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
int l = 0,r = n;
while(l < r){
int mid = (l + r + 1) >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
di(l);
return 0;
}