题解:CF2085D Serval and Kaitenzushi Buffet

· · 题解

由题意,拿 1 盘等价于消耗 1 分钟拿取,并消耗 k 分钟食用,即需要 k+1 分钟。

倒着思考,第 i 分钟,即还剩 n-i+1 分钟时,那么至多还能食用 \frac{n+i-1}{k+1} 盘寿司。

用一个堆维护当前食用的寿司美味值最小值,如果堆的大小超出了剩余可食用数,弹出最小值即可。

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N=2e5+5;
int t;
int n,k;
ll a[N];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> t;
    while(t--){
        cin >> n >> k;
        for(int i=1;i<=n;i++) cin >> a[i];

        priority_queue<ll,vector<ll>,greater<ll>> pq;
        for(int i=n;i>=1;i--){
            pq.push(a[i]);
            ll cnt=(n-i+1)/(k+1);
            while(pq.size()>cnt) pq.pop();
        }

        ll res=0;
        while(pq.size()){
            res+=pq.top();
            pq.pop();
        }

        cout << res << "\n";
    }
    return 0;
}