結果

問題 No.738 平らな農地
ユーザー tentententen
提出日時 2023-07-29 12:02:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 609 ms / 2,000 ms
コード長 3,879 bytes
コンパイル時間 3,650 ms
コンパイル使用メモリ 98,168 KB
実行使用メモリ 52,092 KB
最終ジャッジ日時 2024-04-16 18:21:40
合計ジャッジ時間 35,443 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,348 KB
testcase_01 AC 55 ms
37,092 KB
testcase_02 AC 55 ms
37,316 KB
testcase_03 AC 55 ms
37,140 KB
testcase_04 AC 55 ms
37,496 KB
testcase_05 AC 111 ms
40,136 KB
testcase_06 AC 132 ms
41,576 KB
testcase_07 AC 128 ms
41,188 KB
testcase_08 AC 84 ms
38,592 KB
testcase_09 AC 66 ms
37,532 KB
testcase_10 AC 58 ms
37,136 KB
testcase_11 AC 86 ms
38,736 KB
testcase_12 AC 77 ms
37,500 KB
testcase_13 AC 120 ms
39,864 KB
testcase_14 AC 73 ms
38,884 KB
testcase_15 AC 533 ms
51,320 KB
testcase_16 AC 508 ms
51,456 KB
testcase_17 AC 477 ms
49,556 KB
testcase_18 AC 489 ms
49,656 KB
testcase_19 AC 403 ms
49,556 KB
testcase_20 AC 488 ms
51,316 KB
testcase_21 AC 488 ms
50,468 KB
testcase_22 AC 501 ms
50,940 KB
testcase_23 AC 474 ms
50,572 KB
testcase_24 AC 412 ms
49,600 KB
testcase_25 AC 79 ms
38,848 KB
testcase_26 AC 85 ms
38,768 KB
testcase_27 AC 77 ms
38,816 KB
testcase_28 AC 78 ms
38,592 KB
testcase_29 AC 76 ms
38,976 KB
testcase_30 AC 78 ms
38,908 KB
testcase_31 AC 86 ms
38,776 KB
testcase_32 AC 86 ms
38,900 KB
testcase_33 AC 87 ms
38,640 KB
testcase_34 AC 79 ms
38,476 KB
testcase_35 AC 87 ms
39,064 KB
testcase_36 AC 78 ms
38,888 KB
testcase_37 AC 77 ms
39,024 KB
testcase_38 AC 78 ms
38,352 KB
testcase_39 AC 77 ms
38,976 KB
testcase_40 AC 87 ms
38,984 KB
testcase_41 AC 76 ms
38,876 KB
testcase_42 AC 86 ms
38,768 KB
testcase_43 AC 87 ms
38,120 KB
testcase_44 AC 79 ms
38,240 KB
testcase_45 AC 552 ms
51,560 KB
testcase_46 AC 609 ms
51,564 KB
testcase_47 AC 517 ms
51,204 KB
testcase_48 AC 494 ms
51,436 KB
testcase_49 AC 490 ms
51,612 KB
testcase_50 AC 521 ms
51,336 KB
testcase_51 AC 508 ms
51,136 KB
testcase_52 AC 512 ms
51,428 KB
testcase_53 AC 496 ms
51,704 KB
testcase_54 AC 518 ms
51,348 KB
testcase_55 AC 516 ms
51,672 KB
testcase_56 AC 500 ms
51,484 KB
testcase_57 AC 499 ms
51,252 KB
testcase_58 AC 497 ms
50,900 KB
testcase_59 AC 525 ms
51,692 KB
testcase_60 AC 515 ms
51,404 KB
testcase_61 AC 504 ms
51,156 KB
testcase_62 AC 503 ms
51,172 KB
testcase_63 AC 532 ms
51,152 KB
testcase_64 AC 517 ms
51,324 KB
testcase_65 AC 260 ms
48,272 KB
testcase_66 AC 284 ms
48,508 KB
testcase_67 AC 415 ms
51,172 KB
testcase_68 AC 430 ms
51,144 KB
testcase_69 AC 416 ms
50,008 KB
testcase_70 AC 480 ms
52,092 KB
testcase_71 AC 324 ms
49,132 KB
testcase_72 AC 431 ms
50,428 KB
testcase_73 AC 432 ms
50,716 KB
testcase_74 AC 406 ms
49,808 KB
testcase_75 AC 441 ms
50,104 KB
testcase_76 AC 470 ms
50,844 KB
testcase_77 AC 435 ms
50,024 KB
testcase_78 AC 440 ms
50,116 KB
testcase_79 AC 420 ms
50,156 KB
testcase_80 AC 440 ms
50,768 KB
testcase_81 AC 409 ms
50,372 KB
testcase_82 AC 390 ms
49,960 KB
testcase_83 AC 405 ms
50,544 KB
testcase_84 AC 440 ms
51,244 KB
testcase_85 AC 287 ms
49,672 KB
testcase_86 AC 294 ms
49,348 KB
testcase_87 AC 388 ms
49,560 KB
testcase_88 AC 368 ms
49,456 KB
testcase_89 AC 56 ms
37,228 KB
testcase_90 AC 55 ms
37,340 KB
testcase_91 AC 54 ms
37,216 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