51Nod 1686 第K大区间 (尺取+二分

题意:
很棒的一道题

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值