Submission #37169694


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int64_t> A(N);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }

  vector<int64_t> sorted(M);
  for (int m = 0; m < M; ++m) {
    sorted[m] = A[m];
  }
  sort(sorted.begin(), sorted.end());
  int64_t ans = accumulate(sorted.begin(), sorted.begin() + K, 0LL);
  cout << ans;
  for (int i = 0; i + M < N; ++i) {
    auto it = lower_bound(sorted.begin(), sorted.end(), A[i+M]);
    if (distance(sorted.begin(), it) < K) {
      ans += A[i+M];
      ans -= *(sorted.begin() + K - 1);
    }
    sorted.insert(it, A[i+M]);

    it = lower_bound(sorted.begin(), sorted.end(), A[i]);
    if (distance(sorted.begin(), it) < K) {
      ans -= A[i];
      ans += *(sorted.begin() + K);
    }
    sorted.erase(it);
    cout << " " << ans;
  }
  cout << endl;
}

Submission Info

Submission Time
Task E - Least Elements
User Steinberg
Language C++ (GCC 9.2.1)
Score 500
Code Size 893 Byte
Status AC
Exec Time 1446 ms
Memory 7040 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 21
Set Name Test Cases
Sample 00_example_00.txt, 00_example_01.txt
All 00_example_00.txt, 00_example_01.txt, 01_max_00.txt, 01_max_01.txt, 02_min_00.txt, 03_m_small_00.txt, 04_random_00.txt, 04_random_01.txt, 04_random_02.txt, 04_random_03.txt, 04_random_04.txt, 04_random_05.txt, 04_random_06.txt, 04_random_07.txt, 04_random_08.txt, 04_random_09.txt, 04_random_10.txt, 04_random_11.txt, 04_random_12.txt, 04_random_13.txt, 04_random_14.txt
Case Name Status Exec Time Memory
00_example_00.txt AC 8 ms 3628 KiB
00_example_01.txt AC 2 ms 3612 KiB
01_max_00.txt AC 76 ms 6348 KiB
01_max_01.txt AC 1446 ms 6356 KiB
02_min_00.txt AC 4 ms 3452 KiB
03_m_small_00.txt AC 85 ms 4684 KiB
04_random_00.txt AC 93 ms 4104 KiB
04_random_01.txt AC 758 ms 4972 KiB
04_random_02.txt AC 366 ms 4420 KiB
04_random_03.txt AC 483 ms 4680 KiB
04_random_04.txt AC 243 ms 5832 KiB
04_random_05.txt AC 696 ms 5192 KiB
04_random_06.txt AC 99 ms 4888 KiB
04_random_07.txt AC 877 ms 7040 KiB
04_random_08.txt AC 35 ms 3892 KiB
04_random_09.txt AC 20 ms 3760 KiB
04_random_10.txt AC 213 ms 5664 KiB
04_random_11.txt AC 162 ms 4256 KiB
04_random_12.txt AC 137 ms 3832 KiB
04_random_13.txt AC 211 ms 4272 KiB
04_random_14.txt AC 11 ms 3688 KiB