結果

問題 No.738 平らな農地
ユーザー tentententen
提出日時 2023-07-29 12:02:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 3,879 bytes
コンパイル時間 3,571 ms
コンパイル使用メモリ 89,816 KB
実行使用メモリ 63,736 KB
最終ジャッジ日時 2024-10-07 17:51:20
合計ジャッジ時間 31,603 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
50,308 KB
testcase_01 AC 52 ms
50,480 KB
testcase_02 AC 51 ms
50,496 KB
testcase_03 AC 51 ms
50,644 KB
testcase_04 AC 51 ms
50,156 KB
testcase_05 AC 114 ms
52,844 KB
testcase_06 AC 118 ms
54,044 KB
testcase_07 AC 117 ms
53,604 KB
testcase_08 AC 77 ms
52,040 KB
testcase_09 AC 58 ms
50,636 KB
testcase_10 AC 56 ms
50,416 KB
testcase_11 AC 92 ms
51,948 KB
testcase_12 AC 77 ms
51,200 KB
testcase_13 AC 110 ms
53,632 KB
testcase_14 AC 75 ms
51,488 KB
testcase_15 AC 423 ms
61,204 KB
testcase_16 AC 446 ms
61,392 KB
testcase_17 AC 418 ms
61,416 KB
testcase_18 AC 428 ms
61,432 KB
testcase_19 AC 386 ms
59,628 KB
testcase_20 AC 443 ms
61,584 KB
testcase_21 AC 458 ms
61,504 KB
testcase_22 AC 464 ms
61,512 KB
testcase_23 AC 421 ms
60,052 KB
testcase_24 AC 380 ms
59,708 KB
testcase_25 AC 81 ms
51,668 KB
testcase_26 AC 79 ms
51,304 KB
testcase_27 AC 78 ms
52,156 KB
testcase_28 AC 80 ms
52,104 KB
testcase_29 AC 83 ms
51,724 KB
testcase_30 AC 77 ms
51,300 KB
testcase_31 AC 80 ms
51,952 KB
testcase_32 AC 71 ms
51,980 KB
testcase_33 AC 81 ms
51,756 KB
testcase_34 AC 70 ms
51,728 KB
testcase_35 AC 78 ms
51,840 KB
testcase_36 AC 77 ms
52,012 KB
testcase_37 AC 67 ms
51,972 KB
testcase_38 AC 79 ms
51,564 KB
testcase_39 AC 82 ms
51,932 KB
testcase_40 AC 81 ms
51,944 KB
testcase_41 AC 82 ms
51,756 KB
testcase_42 AC 82 ms
51,396 KB
testcase_43 AC 81 ms
51,580 KB
testcase_44 AC 67 ms
51,652 KB
testcase_45 AC 471 ms
61,212 KB
testcase_46 AC 460 ms
61,292 KB
testcase_47 AC 488 ms
61,172 KB
testcase_48 AC 435 ms
61,296 KB
testcase_49 AC 453 ms
61,564 KB
testcase_50 AC 471 ms
61,192 KB
testcase_51 AC 472 ms
61,792 KB
testcase_52 AC 504 ms
61,684 KB
testcase_53 AC 467 ms
61,792 KB
testcase_54 AC 460 ms
61,544 KB
testcase_55 AC 468 ms
61,272 KB
testcase_56 AC 456 ms
61,820 KB
testcase_57 AC 448 ms
61,316 KB
testcase_58 AC 448 ms
61,932 KB
testcase_59 AC 500 ms
61,444 KB
testcase_60 AC 456 ms
61,508 KB
testcase_61 AC 457 ms
61,344 KB
testcase_62 AC 465 ms
61,616 KB
testcase_63 AC 508 ms
61,820 KB
testcase_64 AC 483 ms
61,592 KB
testcase_65 AC 265 ms
58,920 KB
testcase_66 AC 266 ms
58,952 KB
testcase_67 AC 401 ms
61,488 KB
testcase_68 AC 405 ms
61,472 KB
testcase_69 AC 388 ms
61,836 KB
testcase_70 AC 436 ms
63,736 KB
testcase_71 AC 327 ms
59,360 KB
testcase_72 AC 381 ms
61,240 KB
testcase_73 AC 374 ms
61,080 KB
testcase_74 AC 384 ms
61,740 KB
testcase_75 AC 442 ms
61,752 KB
testcase_76 AC 423 ms
62,076 KB
testcase_77 AC 374 ms
59,760 KB
testcase_78 AC 398 ms
61,960 KB
testcase_79 AC 400 ms
62,116 KB
testcase_80 AC 421 ms
61,620 KB
testcase_81 AC 368 ms
61,484 KB
testcase_82 AC 337 ms
59,500 KB
testcase_83 AC 391 ms
61,524 KB
testcase_84 AC 410 ms
61,460 KB
testcase_85 AC 264 ms
59,524 KB
testcase_86 AC 263 ms
59,268 KB
testcase_87 AC 357 ms
59,444 KB
testcase_88 AC 373 ms
60,868 KB
testcase_89 AC 53 ms
50,484 KB
testcase_90 AC 52 ms
50,376 KB
testcase_91 AC 52 ms
50,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        Place[] places = new Place[n];
        PriorityQueue<Place> queue = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            places[i] = new Place(i, sc.nextInt());
            if (i < k) {
                queue.add(places[i]);
            }
        }
        TreeSet<Place> front = new TreeSet<>();
        long frontSum = 0; 
        for (int i = 0; i < k / 2; i++) {
            Place x = queue.poll();
            frontSum += x.value;
            front.add(x);
        }
        TreeSet<Place> rear = new TreeSet<>();
        long rearSum = 0;
        while (queue.size() > 0) {
            Place x = queue.poll();
            rearSum += x.value;
            rear.add(x);
        }
        long mid = rear.first().value;
        long ans = mid * front.size() - frontSum + rearSum - mid * rear.size();
        for (int i = k; i < n; i++) {
            if (front.contains(places[i - k])) {
                front.remove(places[i - k]);
                frontSum -= places[i - k].value;
                rear.add(places[i]);
                rearSum += places[i].value;
                Place x = rear.pollFirst();
                rearSum -= x.value;
                frontSum += x.value;
                front.add(x);
            } else {
                rear.remove(places[i - k]);
                rearSum -= places[i - k].value;
                front.add(places[i]);
                frontSum += places[i].value;
                Place x = front.pollLast();
                frontSum -= x.value;
                rearSum += x.value;
                rear.add(x);
            }
            mid = rear.first().value;
            ans = Math.min(ans, mid * front.size() - frontSum + rearSum - mid * rear.size());
        }
        System.out.println(ans);
    }
    
    static class Place implements Comparable<Place> {
        int idx;
        int value;
        
        public Place(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Place another) {
            if (value == another.value) {
                return idx - another.idx;
            } else {
                return value - another.value;
            }
        }
        
        public int hashCode() {
            return idx;
        }
        
        public boolean equals(Object o) {
            return hashCode() == o.hashCode();
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0