题解:CF2259G Index Removal

· · 题解

观察题。。。

对于一个被删除的下标 i,我们只需要对右边元素进行操作就行(显然对左边操作一定不优),所以方案就是:

由于 a_j - a_{j-1} \le k,所以 a_j=\min\{a_j,a_{j-1}+(j-i)\times k\},所以当 a_j > a_i + (j-i) \times k 时,需要对 j 进行修改。

令 c_i=a_i-i\times k,所以上面的条件可以变为:c_j>c_i-k。

由于原数组是“好数组”,所以有 c_j\le c_{j-1},所以我们可以二分找这个段结束的位置计算答案即可。

#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
namespace CZW {
#define endl "\n"
#define vec std::vector
#define pb push_back
#define eb emplace_back
    using ll = long long;
    using ull = unsigned long long;
    using i128 = __int128;
    using lb = long double;
    void init() {
        //      freopen("1.in","r",stdin);
        //      freopen("my.out","w",stdout);
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
    }
    void Main() {
        int n;
        ll k;
        cin >> n >> k;
        vec<ll> a(n + 1);
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        vec<ll> c(n + 1, 0);
        for (int i = 1; i <= n; ++i)
            c[i] = a[i] - i * k;
        vec<ll> pre(n + 1, 0);
        for (int i = 1; i <= n; ++i)
            pre[i] = pre[i - 1] + c[i];
        vec<ll> ans(n + 1, 0);
        for (int i = 2; i < n; ++i) {
            ll x = c[i - 1] - k;
            int idx = lower_bound(c.begin() + i + 1, c.end(), x, greater()) -
                      c.begin();
            if (idx > i + 1) {
                int cnt = idx - i - 1;
                ans[i] = pre[idx - 1] - pre[i] - (i128)cnt * x;
            }
        }
        for (int i = 1; i <= n; ++i)
            cout << ans[i] << ' ';
        cout << endl;
    }
} // namespace CZW
int main() {
    CZW::init();
    int Test = 1;
    cin >> Test;
    while (Test--)
        CZW::Main();
    return 0;
}