提出 #70447345


ソースコード 拡げる

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        long M = Long.parseLong(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        
        long[] A = new long[N];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            A[i] = Long.parseLong(st.nextToken());
        }
        
        Arrays.sort(A);
        
        ArrayList<Long> pos = new ArrayList<>();
        ArrayList<Integer> cnt = new ArrayList<>();
        
        pos.add(A[0]);
        int cur = 1;
        
        for (int i = 1; i < N; i++) {
            if (A[i] == A[i-1]) {
                cur++;
            } else {
                cnt.add(cur);
                pos.add(A[i]);
                cur = 1;
            }
        }
        cnt.add(cur);
        
        int K = pos.size();
        int[] pref = new int[2 * K + 1];
        
        for (int i = 0; i < K; i++) {
            pref[i + 1] = pref[i] + cnt.get(i);
        }
        for (int i = K; i < 2 * K; i++) {
            pref[i + 1] = pref[i] + cnt.get(i - K);
        }
        
        long ans = 0;
        
        for (int i = 0; i < K; i++) {
            long sp = pos.get(i);
            long np = pos.get((i + 1) % K);
            
            long len = (np > sp) ? (np - sp) : (M - sp + np);
            
            int meet = 0;
            int low = i + 1;
            int high = i + K;
            int idx = i + K;
            
            while (low <= high) {
                int mid = (low + high) / 2;
                if (pref[mid + 1] - pref[i + 1] >= C) {
                    idx = mid;
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }
            
            meet = pref[idx + 1] - pref[i + 1];
            ans += (long) meet * len;
        }
        
        System.out.println(ans);
    }
}

提出情報

提出日時
問題 D - On AtCoder Conference
ユーザ addy
言語 Java (OpenJDK 17)
得点 425
コード長 2255 Byte
結果 AC
実行時間 599 ms
メモリ 88700 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 425 / 425
結果
AC × 2
AC × 32
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
example_00.txt AC 39 ms 34888 KiB
example_01.txt AC 41 ms 34536 KiB
hand_00.txt AC 225 ms 68948 KiB
hand_01.txt AC 201 ms 68572 KiB
hand_02.txt AC 579 ms 88700 KiB
hand_03.txt AC 193 ms 58816 KiB
hand_04.txt AC 39 ms 34604 KiB
hand_05.txt AC 41 ms 34320 KiB
random_00.txt AC 229 ms 62444 KiB
random_01.txt AC 241 ms 63808 KiB
random_02.txt AC 238 ms 63040 KiB
random_03.txt AC 325 ms 63228 KiB
random_04.txt AC 477 ms 64964 KiB
random_05.txt AC 445 ms 63092 KiB
random_06.txt AC 511 ms 68844 KiB
random_07.txt AC 517 ms 70904 KiB
random_08.txt AC 549 ms 69156 KiB
random_09.txt AC 232 ms 68520 KiB
random_10.txt AC 237 ms 69208 KiB
random_11.txt AC 228 ms 69456 KiB
random_12.txt AC 246 ms 69724 KiB
random_13.txt AC 270 ms 70092 KiB
random_14.txt AC 277 ms 71124 KiB
random_15.txt AC 229 ms 70992 KiB
random_16.txt AC 292 ms 70572 KiB
random_17.txt AC 301 ms 70788 KiB
random_18.txt AC 265 ms 71652 KiB
random_19.txt AC 481 ms 71776 KiB
random_20.txt AC 429 ms 70884 KiB
random_21.txt AC 479 ms 75664 KiB
random_22.txt AC 574 ms 75592 KiB
random_23.txt AC 599 ms 87936 KiB