57022023-09-09 17:40:22TomaSajtSípálya (55 pont)cpp17Accepted 55/5532ms12728 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<ll> range_maxes(const vector<ll>& v, int k) {
  int n = v.size();
  vector<ll> res(n - k + 1);
  deque<int> dq;
  for (int i = 0; i < n; i++) {
    while (!dq.empty() && i - dq.front() >= k) dq.pop_front();
    while (!dq.empty() && v[dq.back()] <= v[i]) dq.pop_back();
    dq.push_back(i);
    if (i - k + 1 >= 0) {
      res[i - k + 1] = v[dq.front()];
    }
  }
  return res;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(0);

  int n, k;
  cin >> n >> k;

  vector<ll> v(n);
  for (auto& a : v) cin >> a;
  for (int i = 0; i < n; i++) v[i] += i;  // add the index, so that we now need to make flat regions instead of stairs

  vector<ll> maxes = range_maxes(v, k);  // maxes[i]: max value in the i-th K length interval

  vector<ll> ps(n + 1);
  partial_sum(v.begin(), v.end(), ps.begin() + 1);

  ll best = LLONG_MAX;
  for (int i = 0; i <= n - k; i++) {
    ll sum = ps[i + k] - ps[i];  // sum of values in the i-th K length interval
    ll cost = k * maxes[i] - sum;
    best = min(best, cost);
  }
  cout << best;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1824 KiB
2Accepted0/03ms2056 KiB
3Accepted2/23ms2280 KiB
4Accepted2/23ms2508 KiB
5Accepted2/23ms2464 KiB
6Accepted2/23ms2692 KiB
7Accepted3/33ms2908 KiB
8Accepted1/14ms3464 KiB
9Accepted1/14ms3572 KiB
10Accepted1/14ms3668 KiB
11Accepted1/14ms3792 KiB
12Accepted1/14ms3788 KiB
13Accepted1/14ms3652 KiB
14Accepted2/24ms3636 KiB
15Accepted2/24ms3668 KiB
16Accepted2/230ms12700 KiB
17Accepted2/229ms11200 KiB
18Accepted2/229ms11200 KiB
19Accepted3/329ms10452 KiB
20Accepted2/230ms12264 KiB
21Accepted2/230ms12660 KiB
22Accepted2/230ms12620 KiB
23Accepted2/232ms12728 KiB
24Accepted2/232ms12728 KiB
25Accepted2/230ms12332 KiB
26Accepted2/229ms12332 KiB
27Accepted2/230ms12336 KiB
28Accepted3/332ms12360 KiB
29Accepted3/330ms12352 KiB
30Accepted3/330ms12564 KiB