結果
| 問題 | No.738 平らな農地 | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2023-07-29 12:02:17 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                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 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 87 | 
ソースコード
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();
    }
    
}
            
            
            
        