CF1618G Trader Problem 题解

题目链接:CF 或者 洛谷

视频讲解

本题挺有意思的,我们观察到 \(\le k\) 这个限制使得我们可以将原序列进行分组,把 \(\le k\) 的限制的元素放在一组中,那么根据题意,这组当中任意元素之间都是可以互相交换的,包括系统用品。那么假设一组中有 \(x\) 个自身的物品,\(y\) 个系统物品,那么这 \(x+y\) 物品 都能实现互相交换,但只有 \(x\) 个物品才能作为贡献,根据贪心要求答案最大,那么我们需要这 \(x+y\) 物品中最大的 \(x\) 个物品作为贡献。接下来我们观察到物品这玩意是需要贪心取的,为我们先对物品(这里包括自身物品和系统物品)按照价值排序,对于一个 \(k\) 限制,我们观察到这玩意有一个类似单调性的性质:

\(k\) 越大,那么组数越少,会导致一些组进行合并,合并的要求就是组的极值元素满足 \(\le k\) 限制,\(k\) 越小,显然还要拆分组,那么我们不妨让询问的 \(k\) 变得有序。这样我们的组就是不断合并的过程。接下来考虑 \(k\) 限制会让一些区间合并为一组,这些区间的特点为:\(max-min \le k\),我们可以通过枚举单点预处理出这些连接点所需要的 \(k\),并将它有序排序,这样一来,我们就可以知道需要合并的 \([pos,pos+1]\) 的相邻点对。接下来考虑答案贡献变化,在原来总和的情况,合并意味着需要去掉两个组各自的贡献,然后加上新的组贡献:

贡献为 \(x+y\) 数中的最大的 \(x\) 数之和,组这个的东西我们可以并查集维护,这样一来就知道 \(x\) 大小,关于这 \(x\) 个数最大和,由于组这个东西是连续的区间,那么又因为我们按照了 \(val\) 排序了,所以这里并不太需要什么数据结构或者启发式合并,我们只需要使用前缀和就能知道后缀 \(x\) 个数之和,这个后缀就是最大的 \(x\) 个数。为了找到一个组的后缀右端点,我们并查集可以同时维护最右端点。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

// #define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 1e6 + 10;
ll pre[N];
pii a[N];

struct Op
{
    int pos, k;

    bool operator<(const Op& other) const
    {
        return k < other.k;
    }
} op[N];

int opCnt;
pii k[N];
int n, m, q;
ll curr;
ll ans[N];
int fa[N], siz[N], last[N];

inline int find(const int x)
{
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}

inline void merge(const int x, const int y)
{
    fa[x] = y, siz[y] += siz[x], uMax(last[y], last[x]);
}

inline void solve()
{
    cin >> n >> m >> q;
    forn(i, 1, n+m)cin >> a[i].first, a[i].second = i, curr += (i <= n) * a[i].first;
    sortArr(a, n+m);
    forn(i, 1, n+m)fa[i] = last[i] = i, siz[i] = a[i].second <= n, pre[i] += pre[i - 1] + a[i].first;
    forn(i, 1, n+m-1)op[++opCnt] = Op(i, a[i + 1].first - a[i].first);
    sortArr(op, opCnt);
    forn(i, 1, q)cin >> k[i].first, k[i].second = i;
    sortArr(k, q);
    int updateCnt = 1;
    forn(i, 1, q)
    {
        const auto [diff,id] = k[i];
        while (updateCnt <= opCnt and op[updateCnt].k <= diff)
        {
            const auto [pos,_] = op[updateCnt];
            const int x = find(pos), y = find(pos + 1);
            const ll leftSum = pre[last[x]] - pre[last[x] - siz[x]];
            const ll rightSum = pre[last[y]] - pre[last[y] - siz[y]];
            curr -= leftSum + rightSum;
            merge(x, y);
            curr += pre[last[y]] - pre[last[y] - siz[y]];
            updateCnt++;
        }
        ans[id] = curr;
    }
    forn(i, 1, q)cout << ans[i] << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[忽略并查集的反阿克曼函数常数,时间复杂度为:\ O((n+m)\log{(n+m)}+q\log{q}) \]

posted @ 2024-03-22 20:34  Athanasy  阅读(7)  评论(0编辑  收藏  举报