跳到正文

题解

题解:CF2259G Index Removal

Subway_hub约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年9月7日
收录
2026年10月15日
更新
2026年11月11日
篇幅
约 4 分钟
获赞
1
题目
CF2259G

版本

  1. v1当前

观察题。。。

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

  • ai+1=min⁡{ai+1,ai−1+k}a_{i+1}=\min \{a_{i+1},a_{i-1}+k\}

  • ∀j≥i+2, aj=min⁡{aj,aj−1+k}\forall j \ge i + 2,\ a_j = \min \{a_{j},a_{j-1}+k\}

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

令 ci=ai−i×kc_i=a_i-i\times k,所以上面的条件可以变为:cj>ci−kc_j>c_i-k。

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

C++
#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;
}
原文