题解:CF2259G Index Removal
Subway_hub · · 题解
观察题。。。
对于一个被删除的下标
-
a_{i+1}=\min \{a_{i+1},a_{i-1}+k\} -
\forall j \ge i + 2,\ a_j = \min \{a_{j},a_{j-1}+k\}
由于
令
由于原数组是“好数组”,所以有
#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;
}