CF2248G No Balance Left

· · 题解

一道综合能力要求非常高的上位紫。

本篇题解参考了官解的写法,并适当地进行了一些补充,故变量名基本与官解一致。

Thoughts:

题意不复杂,就是一个很常见的满 a_i 减 b_i 的促销模型,只是这里 b_i 可以大于 a_i 。

先剧透一下,这个性质在后面的分析中具有关键性的作用。

考虑最后一笔支付,显然这笔支付不应该有任何返利,否则余额就无法清空。

题目保证了 a_i 和 b_i 是严格单调递增的,并且都是正整数,故只需要判断最小的支付金额 c_1 和 a_1 的关系即可。

若 c_1 \ge a_1 ,那么无论余额是多少都不可能清空,全部输出 \texttt{NO} 即可。

剩下来要分析一般的情况。

首先在不考虑余额的情况下能够支付的价格是一个关于 c_i 的可行性完全背包,

这里需要 bitset 优化到 \Theta (\cfrac{V^2}{\omega}) ,时限开了 6 秒绰绰有余。

然后考虑对于每一个能够支付的价格会得到的返利,设为 r_i ,显然有 r_i = \max\limits_{a_j \le i}\{ b_j \} ,存在单调性,在 a_i 上双指针即可。

根据 r_i 的单调性可得,只需要判断最小的支付金额 c_1 和 r_{c_i} 的关系即可。

然后分类讨论 r_i 和 i 的关系。

代码难度其实并不高,注释去掉只有 70 行左右,主要难点在于分析。

Analyses:

总时间复杂度 \Theta (\cfrac{V^2}{\omega}) 。

总空间复杂度 \Theta (N + M + V) 。

:::success[Code]

/* construct : AUG 21'26 */
#include    <algorithm>
#include    <iostream>
#include    <numeric>
#include    <bitset>
using namespace std;
constexpr int maxn = 2e5+4;

int n, m, s;
int a[maxn];
int b[maxn];
int c[maxn];
int r[maxn];
// int l[maxn];
bitset<maxn> dp, ans;

signed main (void)
{
    cin >> n >> m >> s;
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    sort (c + 1, c + n + 1);
    for (int i = 1; i <= m; i++)
        cin >> a[i] >> b[i];
    ans[0] = 1;
    if (c[1] < a[1])
    {
        dp[0] = 1;
        int V = max (s, a[m]);
        for (int i = 1; i <= n; i++)
            dp[c[i]] = 1;
        for (int i = 1; i <= V; i++) if (dp[i])
            dp |= dp << i;
        int pos = 0;
        int mnip = 0;
        for (int i = 1; i <= V; i++) if (dp[i])
        {
            while (pos < m && a[pos + 1] <= i)
                pos++;
            r[i] = b[pos];
            if (!mnip)
            {
                if (r[i] > i)
                    mnip = i;
                else
                    ans |= ans >> r[i] << i;
            }
        }
        if (!mnip)
            mnip = V + 1;
        // cout << mnip << endl;
        // fill (l + 1, l + mnip + 1, V + 1);
        // for (int i = 1; i < mnip; i++)
        //  l[i - r[i]] = min (l[i - r[i]], i);
        // for (int i = 1; i <= n && c[i] < mnip; i++)
        //  l[c[i] - r[c[i]]] = min (l[c[i] - r[c[i]]], c[i]);
        // cout << mnip << endl;
        // for (int i = 1; i < mnip; i++)
        //  cout << l[i] << ' ';
        // cout << endl;
        // for (int i = 1; i < mnip; i++)
        // {
        //  for (int d = 1; d <= i; d++) if (ans[i - d] && l[d] <= i)
        //  {
        //      ans[i] = 1;
        //      break;
        //  }
        // }
        // for (int d = 1; d < mnip; d++)
        // {
        //  for (int i = l[d]; i < mnip; i++) if (dp[i] & ans[i - d])
        //  {
        //      ans[i] = 1;
        //      break;
        //  }
        // }
        for (int i = 1; i < mnip; i++) if (ans[i])
            ans |= ans << i;
        int g = b[m];
        for (int i = 1; i <= n; i++)
            g = gcd (g, c[i]);
        for (int i = 1; i <= V; i++) if (dp[i] && r[i] != i)
            g = gcd (g, abs (i - r[i]));
        // cout << g << endl;
        for (int i = mnip; i <= s; i++)
            ans[i] = !(i % g);
    }
    for (int i = 1; i <= s; i++)
    {
        if (ans[i])
            puts ("YES");
        else
            puts ("NO");
    }
    return 0;
}

:::