zoukankan      html  css  js  c++  java
  • Gym 101194D Ice Cream Tower (二分查找 + 贪心)

    用贪心判断当前个数是否成立, 然后二分查找搜答案

    #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
    ",l);
        }
    }
  • 相关阅读:
    两种称谓
    HDU 1074

    Educational Codeforces Round 44
    nowcoder—Beauty of Trees
    nowcoder-练习赛16
    c++作业-8
    差的东西
    nowcoder-挑战赛14
    BZOJ2548 [CTSC2002] 灭鼠行动
  • 原文地址:https://www.cnblogs.com/YY666/p/11226986.html
Copyright © 2011-2022 走看看