题解:P17877 糖葫芦 / tanghulu

· · 题解

太深刻了喵。

首先贪心策略比较好想,对于一个东西,找刚好大于她的就行了。

合并显然要合并最大的、且两个都 >k 的。

然后考虑维护。

首先有暴力。很好写。

然后你发现似乎对剩余量分组是一个很好的优化。也就是把量相等的看做一组。

……然后你就会发现她是对的。

结论 1:每次做糖葫芦的消耗最多导致 O(1) 个组的产生。

这个比较好理解。就是即使消耗很多,但是大部分组都是被消耗得干干净净为 b_i-1,只有 1 个组会消耗部分。

增加量最多为 2(b_i-1 组也要算)。

所以组数为 O(n+m)。

然后考虑合并。你是每次取最大的两个合并。

显然合并了就没了。

因此:

结论 2:合并的循环轮数为 O(n+m)。

现在继续考虑做糖葫芦的过程。

对于完全删除部分:

而第一种情况每个 (b_i,c_i) 只发生一次,O(m)。

第二个情况删除一个组,而组数不超过 O(n+m)。

对于不完全删除部分,显然是 O(m)。

因此:

结论 3:合并轮数为 O(n+m)。

#include<bits/stdc++.h>
using namespace std;
#define fir(i, l, r) for(int i = l; i <= (int) r; i++)
#define fpr(i, r, l) for(int i = r; i >= (int) l; i--)
#define full(v) v.begin(), v.end()
#define endl '\n'
using ll = long long;
map <ll, ll> mp;
template <typename T, int INBUF = 100000, int OBUF = 2000000, int __LG = 22>
struct IO{
    char *p1 = 0, *p2 = 0, buf[INBUF], obuf[OBUF], *op = obuf;
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, INBUF, stdin), p1 == p2) ? EOF : *p1++)
    __attribute__((always_inline)) T read(){
        register T x = 0; register char ch = gc();
        while(ch < '0' || ch > '9') ch = gc(); 
        while(ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
        return x;
    }
    __attribute__((always_inline)) void write_with(T _ans, char ch = '\n'){
        if(__builtin_expect(! _ans, 0)){*op++ = 48, *op++ = ch; return ;}
        static char s[__LG], t = 0;
        while(_ans) s[t++] = _ans % 10 + 48, _ans /= 10;
        while(t) *op++ = s[--t]; *op++ = ch;
    }
    __attribute__((always_inline)) void wchar(char ch){*op++ = ch;}
    __attribute__((always_inline)) void flush(){fwrite(obuf, 1, op - obuf, stdout);}
};
IO <ll> flow;
signed main(){
    ios :: sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
    ll C, t, a, b, c, n, m, k;
    C = flow.read(), t = flow.read();
    while(t--){
        n = flow.read(), m = flow.read(), k = flow.read();
        mp.clear();
        fir(i, 1, n) mp[-flow.read()]++;
        bool pd = 1;
        ll ans = 0;
        fir(i, 1, m){
            b = flow.read(), c = flow.read();
            if(! pd) continue ;
            while(c){
                auto it = mp.upper_bound(-b);
                if(it == mp.begin()){
                    basic_string <ll> e;
                    ll sum = k, sz, dd, o;
                    for(auto [ssiz, cnt] : mp){
                        ll siz = -ssiz;
                        if(sum + (siz - k) * cnt < b){
                            sum += (siz - k) * cnt;
                            e += siz;
                            ans += cnt;
                            continue ;
                        }
                        else if(siz > k){
                            ll d = (b - sum + siz - k - 1) / (siz - k);
                            ans += d;
                            o = cnt, sz = siz, dd = d;
                            sum += (siz - k) * d;
                            break ;
                        } 
                        else break ;
                    }
                    ans--;
                    if(sum < b){pd = 0; break ;}
                    for(auto x : e) mp.erase(-x);
                    mp[-sz] = o - dd;
                    if(! mp[-sz]) mp.erase(-sz);
                    mp[-sum]++;
                }
                else{
                    auto [ssiz, cnt] = *--it;
                    ll siz = -ssiz, x = siz - b + 1;//siz大小做x个
                    if(x * cnt < c) c -= x * cnt, mp.erase(-siz), mp[-b + 1] += cnt;
                    else{//够
                        mp[-b + 1] += c / x;
                        mp[-siz] -= c / x + (c % x > 0);
                        if(mp[-siz] == 0) mp.erase(-siz);
                        if(c % x) mp[-siz + c % x]++;
                        c = 0;
                    }
                }
            }
        }
        if(! pd) flow.wchar(45), flow.wchar(49), flow.wchar(10);
        else flow.write_with(ans, 10);
    }
    flow.flush();
    return 0;
}