結果

問題 No.68 よくある棒を切る問題 (2)
ユーザー tentententen
提出日時 2021-01-27 19:21:33
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,456 bytes
コンパイル時間 2,579 ms
コンパイル使用メモリ 76,428 KB
実行使用メモリ 104,220 KB
最終ジャッジ日時 2023-09-07 02:41:37
合計ジャッジ時間 30,565 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        TreeSet<Stick> sticks = new TreeSet<>();
        TreeSet<Stick> nexts = new TreeSet<Stick>(new Comparator<Stick>() {
            public int compare(Stick s1, Stick s2) {
                if (s1.idx == s2.idx) {
                    return 0;
                } else {
                    double diff = s1.value / s1.count  - s2.value / s2.count;
                    if (diff == 0) {
                        return s1.idx - s2.idx;
                    } else if (diff < 0) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
        });
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            Stick s = new Stick(i, arr[i]);
            sticks.add(s);
            nexts.add(s);
        }
        Arrays.sort(arr);
        int q = sc.nextInt();
        Question[] questions = new Question[q];
        for (int i = 0; i < q; i++) {
            questions[i] = new Question(i, sc.nextInt());
        }
        Arrays.sort(questions);
        double[] ans = new double[q];
        int count = n;
        for (int i = 0; i < q; i++) {
            if (questions[i].value <= n) {
                ans[questions[i].idx] = arr[n - questions[i].value];
            } else {
                while (count < questions[i].value) {
                    Stick s = sticks.pollLast();
                    nexts.remove(s);
                    s.add();
                    sticks.add(s);
                    nexts.add(s);
                    count++;
                }
                ans[questions[i].idx] = nexts.first().getValue();
            }
        }
        StringBuilder sb = new StringBuilder();
        for (double x : ans) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
    
    static class Stick implements Comparable<Stick> {
        int idx;
        double value;
        int count = 1;
        
        public Stick(int idx, double value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int hashCode() {
            return idx;
        }
        
        public boolean equals(Object o) {
            Stick s = (Stick)o;
            return idx == s.idx;
        }
        
        public int compareTo(Stick another) {
            if (idx == another.idx) {
                return 0;
            } else {
                double diff = value / (count + 1)  - another.value / (another.count + 1);
                if (diff == 0) {
                    return idx - another.idx;
                } else if (diff < 0) {
                    return -1;
                } else {
                    return 1;
                }
            }
        }
        
        public double getValue() {
            return value / count;
        }
        
        public void add() {
            count++;
        }
    }
    
    static class Question implements Comparable<Question> {
        int idx;
        int value;
        
        public Question(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Question another) {
            return value - another.value;
        }
    }
}
0