Submission #70542871


Source Code Expand

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

/////////////////// メイン ///////////////////

int main () {
  
  //////////////////// 入力 ////////////////////

  int n, c;
  long long m;
  cin >> n >> m >> c;

  // あとでデータを高速で追加できるように、
  // 2*n+1個分メモリ確保をしてからサイズをnにしておく
  vector<long long> a;
  a.reserve(2*n+1);
  a.resize(n);
  for (int i=0; i<n; i++) {
    cin >> a.at(i);
  }

  //////////////// 出力変数定義 ////////////////

  long long result = 0;

  //////////////////// 処理 ////////////////////

  // ソートして、小屋から2周分、人がいる位置を記録しておく、+3周目の1人目まで(番兵)
  sort(a.begin(),a.end());
  for (int i=0; i<n; i++) a.emplace_back(a.at(i)+m);
  a.emplace_back(a.at(0)+2*m);
  
  // この人の先からスタート、という人の番号
  int l = 0;

  // 最後に合う人の番号
  int r = c;

  // 1周をどこから初めても結果は同じはずなので、
  // 0スタートで1周ではなく、1人目の少し先スタートで1周する
  while (l<n) {
    
    // 最後に合う人の番号を求める
    r = max(r,l+c);
    while (a.at(r+1)==a.at(l+c)) r++;

    // l人目がいる位置とl+1人目がいる位置の間から出発したら、r-l人と会う
    // これをまとめてresultに加算
    result += (a.at(l+1)-a.at(l))*(r-l);

    // 次のlを調査
    l++;

    // 同じところにもう1人いる場合はスキップ
    // (やっても0を足す結果になるだけなので、スキップする必要もないが……)
    while (a.at(l)==a.at(l+1)) l++;

  }

  //////////////////// 出力 ////////////////////

  cout << result << endl;

  //////////////////// 終了 ////////////////////

  return 0;

}

Submission Info

Submission Time
Task D - On AtCoder Conference
User wightou
Language C++ 23 (gcc 12.2)
Score 425
Code Size 1894 Byte
Status AC
Exec Time 184 ms
Memory 11192 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 2
AC × 32
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3416 KiB
example_01.txt AC 1 ms 3456 KiB
hand_00.txt AC 157 ms 10972 KiB
hand_01.txt AC 157 ms 11028 KiB
hand_02.txt AC 182 ms 10996 KiB
hand_03.txt AC 50 ms 10972 KiB
hand_04.txt AC 1 ms 3516 KiB
hand_05.txt AC 1 ms 3568 KiB
random_00.txt AC 73 ms 11116 KiB
random_01.txt AC 72 ms 11020 KiB
random_02.txt AC 76 ms 10980 KiB
random_03.txt AC 107 ms 11192 KiB
random_04.txt AC 109 ms 11032 KiB
random_05.txt AC 107 ms 11120 KiB
random_06.txt AC 123 ms 11072 KiB
random_07.txt AC 124 ms 11068 KiB
random_08.txt AC 123 ms 11184 KiB
random_09.txt AC 151 ms 11092 KiB
random_10.txt AC 157 ms 11020 KiB
random_11.txt AC 148 ms 11048 KiB
random_12.txt AC 147 ms 11032 KiB
random_13.txt AC 166 ms 11092 KiB
random_14.txt AC 168 ms 11072 KiB
random_15.txt AC 161 ms 10984 KiB
random_16.txt AC 173 ms 10976 KiB
random_17.txt AC 173 ms 11072 KiB
random_18.txt AC 161 ms 10964 KiB
random_19.txt AC 181 ms 11072 KiB
random_20.txt AC 175 ms 11044 KiB
random_21.txt AC 173 ms 11000 KiB
random_22.txt AC 184 ms 11048 KiB
random_23.txt AC 182 ms 10996 KiB