题解:CF2200F Mooclear Reactor 2

· · 题解

题解:CF2200F Mooclear Reactor 2

解题思路

考虑分类讨论买来粒子是否是最优答案中限制最严的。

第一个很好维护,记录 \min y_i、\min x_i 即可。

对于 m = 1,第二个情况最多去考虑 n 个粒子并取其中最大的。

考虑如何重复贡献。我们将 m 个粒子按照 y_j 从大到小排序,则每此考虑更小的 y_j 时我们只要向要考虑的粒子集合中添加粒子即可。

可以用优先队列直接维护。

每个粒子最多进出队列一次,队列中最多同时有 n 个粒子,所以总复杂度 \mathcal O((n + m) \log n)。

答案取买了不选和两种情况最大值即可。

参考代码

#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define int long long
using namespace std;
int t , n , m , pl , ans1[200005] , ans2[200005] , qzh[200005] , cnt , ans;
struct p
{
    int x , y;
}a[200005] , b[200005];
priority_queue<int , vector<int> , greater<int>> q;
inline bool cmp(p l , p r)
{
    return l.y > r.y;
}
signed main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        for(int i = 1 ; i <= n ; i++)
        {
            cin >> a[i].x >> a[i].y;
            a[i].y++;
        }
        for(int i = 1 ; i <= m ; i++)
        {
            cin >> b[i].x >> b[i].y;
            b[i].y++;
        }
        sort(a + 1 , a + n + 1 , cmp);
        while(q.size())
        {
            q.pop();
        }
        cnt = ans = 0;
        pl = 1;
        for(int i = n + 1 ; i >= 1 ; i--)
        {
            while(pl <= n && a[pl].y >= i)
            {
                cnt += a[pl].x;
                q.push(a[pl].x);
                pl++;
            }
            while(q.size() > i)
            {
                cnt -= q.top();
                q.pop();
            }
            ans1[i] = cnt;
            ans = max(ans , ans1[i]);
        }
        while(q.size())
        {
            q.pop();
        }
        cnt = 0;
        pl = 1;
        for(int i = n + 1 ; i >= 1 ; i--)
        {
            while(pl <= n && a[pl].y >= i)
            {
                cnt += a[pl].x;
                q.push(a[pl].x);
                pl++;
            }
            while(q.size() >= i)
            {
                cnt -= q.top();
                q.pop();
            }
            ans2[i] = cnt;
        }
        for(int i = 1 ; i <= n + 1 ; i++)
        {
            qzh[i] = max(qzh[i - 1] , ans2[i]);
        }
        for(int i = 1 ; i <= m ; i++)
        {
            cout << max(ans , qzh[b[i].y] + b[i].x) << ' ';
        }
        cout << '\n';
    }
    return 0;
}