結果

問題 No.1012 荷物収集
ユーザー htensaihtensai
提出日時 2020-03-24 14:38:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,342 ms / 2,000 ms
コード長 2,176 bytes
コンパイル時間 3,083 ms
コンパイル使用メモリ 78,972 KB
実行使用メモリ 82,244 KB
最終ジャッジ日時 2024-06-10 05:53:46
合計ジャッジ時間 39,115 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
53,800 KB
testcase_01 AC 116 ms
54,288 KB
testcase_02 AC 126 ms
54,264 KB
testcase_03 AC 117 ms
52,884 KB
testcase_04 AC 1,168 ms
81,856 KB
testcase_05 AC 1,090 ms
81,416 KB
testcase_06 AC 1,124 ms
81,748 KB
testcase_07 AC 1,099 ms
82,124 KB
testcase_08 AC 1,110 ms
82,084 KB
testcase_09 AC 1,154 ms
81,720 KB
testcase_10 AC 1,088 ms
81,312 KB
testcase_11 AC 1,109 ms
82,088 KB
testcase_12 AC 1,110 ms
82,244 KB
testcase_13 AC 1,126 ms
82,100 KB
testcase_14 AC 184 ms
54,616 KB
testcase_15 AC 199 ms
56,784 KB
testcase_16 AC 180 ms
54,360 KB
testcase_17 AC 205 ms
54,924 KB
testcase_18 AC 195 ms
54,532 KB
testcase_19 AC 182 ms
54,408 KB
testcase_20 AC 183 ms
54,596 KB
testcase_21 AC 201 ms
54,516 KB
testcase_22 AC 185 ms
54,716 KB
testcase_23 AC 180 ms
54,820 KB
testcase_24 AC 1,051 ms
75,696 KB
testcase_25 AC 1,257 ms
79,408 KB
testcase_26 AC 940 ms
73,668 KB
testcase_27 AC 523 ms
59,640 KB
testcase_28 AC 1,285 ms
79,544 KB
testcase_29 AC 980 ms
72,480 KB
testcase_30 AC 1,241 ms
76,908 KB
testcase_31 AC 699 ms
64,172 KB
testcase_32 AC 692 ms
62,700 KB
testcase_33 AC 814 ms
68,136 KB
testcase_34 AC 790 ms
65,964 KB
testcase_35 AC 1,130 ms
75,692 KB
testcase_36 AC 728 ms
66,080 KB
testcase_37 AC 954 ms
72,844 KB
testcase_38 AC 1,078 ms
76,720 KB
testcase_39 AC 755 ms
63,040 KB
testcase_40 AC 774 ms
64,840 KB
testcase_41 AC 1,342 ms
80,500 KB
testcase_42 AC 844 ms
67,876 KB
testcase_43 AC 1,020 ms
75,444 KB
testcase_44 AC 126 ms
54,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int q = sc.nextInt();
        Pack[] packs = new Pack[n + 2];
        for (int i = 0; i < n; i++) {
            packs[i] = new Pack(sc.nextInt(), sc.nextInt());
        }
        packs[n] = new Pack(0, 0);
        packs[n + 1] = new Pack(Integer.MAX_VALUE, 0);
        Arrays.sort(packs);
        long[] leftWeight = new long[n + 2];
        long[] leftCosts = new long[n + 2];
        leftWeight[0] = packs[0].weight;
        for (int i = 1; i < n + 2; i++) {
            leftWeight[i] = leftWeight[i - 1] + packs[i].weight;
            leftCosts[i] = leftCosts[i - 1] + (packs[i].point - packs[i - 1].point) * leftWeight[i - 1];
        }
        long[] rightWeight = new long[n + 2];
        long[] rightCosts = new long[n + 2];
        rightWeight[n + 1] = packs[n + 1].weight;
        for (int i = n; i >= 0; i--) {
            rightWeight[i] = rightWeight[i + 1] + packs[i].weight;
            rightCosts[i] = rightCosts[i + 1] + (packs[i + 1].point - packs[i].point) * rightWeight[i + 1];
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            int x = sc.nextInt();
            int left = 0;
            int right = n + 1;
            while (right - left > 1) {
                int m = (left + right) / 2;
                if (packs[m].point <= x) {
                    left = m;
                } else {
                    right = m;
                }
            }
            long ans = leftCosts[left] + leftWeight[left] * (x - packs[left].point) + rightCosts[right] + rightWeight[right] * (packs[right].point - x);
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static class Pack implements Comparable<Pack> {
        int point;
        int weight;
        
        public Pack(int point, int weight) {
            this.point = point;
            this.weight = weight;
        }
        
        public int compareTo(Pack another) {
            return point - another.point;
        }
    }
}
0