zoukankan      html  css  js  c++  java
  • [CF551C] GukiZ hates Boxes

    [CF551C] GukiZ hates Boxes - 二分,贪心

    Description

    有 n 个位置编号 1 到 n,第 i 个位置上有 ai 个箱子,有 m 个人,开始在 0 位置,每秒钟每个人可以选择搬走自己当前位置上的一个箱子或者向前走一步,问至少需要多少时间能把所有箱子搬完。

    Solution

    二分答案加验证,假设有 mid 秒,一个人一个人的处理,每个人尽可能拿最远的那个箱子(反正一定会有人拿那个,先拿不会更劣)

    比如现在我们安排一个人,我们找了最远的箱子给他(假设距离为 dist),那么他已经花费了 dist 时间走路并花费 1 时间搬箱子,剩下的时间可以由远到近处理掉若干个箱子

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    signed main()
    {
        int n, m;
        cin >> n >> m;
    
        vector<int> a(n + 2);
        for (int i = 1; i <= n; i++)
            cin >> a[i];
    
        auto check = [&](vector<int> a, int time) -> bool {
            int pos = n;
            while (pos && a[pos] == 0)
                --pos;
            for (int i = 1; i <= m; i++)
            {
                int hp = time;
                if (hp > pos)
                {
                    hp -= pos;
                    while (pos && hp > 0)
                    {
                        int step = min(a[pos], hp);
                        a[pos] -= step;
                        hp -= step;
                        if (hp == 0)
                            break;
                        --pos;
                    }
                }
            }
            while (pos && a[pos] == 0)
                --pos;
            if (pos == 0)
                return true;
            return false;
        };
    
        int l = 1, r = 1e15;
    
        while (l < r)
        {
            int mid = (l + r) / 2;
            if (check(a, mid))
                r = mid;
            else
                l = mid + 1;
        }
    
        cout << l << endl;
    }
    
  • 相关阅读:
    Java 中几种常用的线程池
    阿里巴巴java工程师面试经验详情
    设计模式-备忘录模式
    设计模式-职责链模式
    设计模式-中介者模式
    设计模式-解释器模式
    设计模式-观察者模式
    设计模式-迭代器模式
    设计模式-命令模式
    设计模式-模板方法模式
  • 原文地址:https://www.cnblogs.com/mollnn/p/14352413.html
Copyright © 2011-2022 走看看