1. 程式人生 > >Ice Cream Tower Gym - 101194D (二分)

Ice Cream Tower Gym - 101194D (二分)

https://cn.vjudge.net/problem/Gym-101194D

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[300005];
int q,n,k;
int vis[300005];
int id[300005];
bool f(ll x)
{
    for(;q<=n; q++)
        if(!vis[q])
        {
            vis[q] = 1;
            return 1;
        }
    return 0;
}
bool ok(int m)
{
    memset(vis,0,sizeof(vis));
    for(int i=1; i<=m; i++)
    {
        id[i] = i;
        vis[i] = 1;
    }
    q = 1;
    for(int i=1; i<k; i++)
    {
        for(int j=1; j<=m; j++)
        {
            q = lower_bound(a+q,a+1+n,2*a[id[j]])-a;
            if(f(2*a[id[j]]) == 0)
                return 0;
            id[j] = q;
        }
    }
    return 1;
}
int main()
{
    ll i,j;
    ll t;
    scanf("%lld",&t);
    int ca = 1;
    while(t--)
    {
        scanf("%d%d",&n,&k);
        for(i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        sort(a+1,a+1+n);
        ll l,r,mid;
        l = 0,r = n/k;
        while(r>l)
        {
            mid =(l+r)/2;
            if(ok(mid))
                l = mid+1;
            else
                r = mid;
        }
        if(!ok(l))
            l--;
        printf("Case #%d: %lld\n",ca++,l);
    }
    return 0;
}