Gym 101194D Ice Cream Tower (+ binary search greedy)

 

Greedy determine whether the current number of the establishment, then the binary search found the answer

 

#include<bits/stdc++.h>
using namespace std;
#define INF ox3f3f3f3f
#define LLU unsigned long long
#pragma GCC optimize(3,"Ofast","inline")
const LLU maxn = 3e5 + 10;
LLU a[maxn],b[maxn];
LLU n, k;
bool check(LLU mid)
{

    for(int i = 0; i < mid; i++)
        b[i] = a[i];

    LLU x = 1,j = 0;
    for(int i = mid; i < n; i++)
    {
        if(a[i] >= b[j] * 2)
        {
            b[j++] = a[i];
            if(j >= mid)
            {
                ++x;
                j = 0;
            }
        }
    }
    return x >= k;
}


int main()
{

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r",stdin);
    freopen("out.txt", "w",stdout);
#endif // ONLINE_JUDGE
    int T;
    cin >> T;
    for(int t = 1; t <= T; t++)
    {
        LLU ans = 0;
        printf("Case #%d: ", t);
        cin >> n >> k;
        for(LLU i = 0; i < n; i++)
            scanf("%lld", &a[i]);
        sort(a, a + n);

        int l = 0, r = n + 1, mid;
        while(l < r - 1)
        {
            mid = (l + r) / 2;
            if(check(mid))
                l = mid;
            else
                r = mid;
        }
        printf("%d\n",l);
    }
}

 

Guess you like

Origin www.cnblogs.com/YY666/p/11226986.html