题意:
很棒的一道题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int stk[70], tp;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e10;
const int N=1e5+15;
int n,k;
int a[N],flag[N];
struct node
{
int a,id,ans;
}po[N];
int cmp(node x,node y)
{
if(x.a==y.a)return x.id<y.id;
return x.a<y.a;
}
int cmp1(node x,node y)
{
return x.id<y.id;
}
int check(int x)
{
memset(flag, 0, sizeof flag);
LL ans=0;
int num=0;
int r=0;
for(int i = 1; i <= n; ++i)
{
while(true)
{
int pos=po[r].ans;
if(pos!=-1) if(flag[pos]>=x||r>n)break;
r++;
pos=po[r].ans;
flag[pos]++;
}
ans=ans+n-(r-1);
flag[po[i].ans]--;
}
if(ans>=k)return 1;
return 0;
}
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> po[i].a;
po[i].id = i;
}
sort(po+1,po+n+1,cmp);
po[0].ans=po[0].a=-1;
for(int i = 1; i <= n; ++i)
{
if(po[i].a!=po[i-1].a)po[i].ans=i;
else po[i].ans=po[i-1].ans;
}
sort(po+1,po+n+1,cmp1);
int l=1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))l=mid+1;
else r=mid-1;
}
cout << l-1 << endl;
return 0;
}