結果

問題 No.1012 荷物収集
ユーザー tentententen
提出日時 2021-11-24 09:07:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,173 ms / 2,000 ms
コード長 3,115 bytes
コンパイル時間 2,420 ms
コンパイル使用メモリ 79,808 KB
実行使用メモリ 91,372 KB
最終ジャッジ日時 2024-06-26 08:17:07
合計ジャッジ時間 33,058 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,516 KB
testcase_01 AC 56 ms
50,384 KB
testcase_02 AC 55 ms
50,468 KB
testcase_03 AC 56 ms
50,180 KB
testcase_04 AC 932 ms
89,048 KB
testcase_05 AC 818 ms
90,028 KB
testcase_06 AC 903 ms
89,704 KB
testcase_07 AC 906 ms
89,168 KB
testcase_08 AC 864 ms
88,912 KB
testcase_09 AC 808 ms
88,976 KB
testcase_10 AC 895 ms
89,228 KB
testcase_11 AC 924 ms
88,948 KB
testcase_12 AC 821 ms
88,956 KB
testcase_13 AC 916 ms
89,728 KB
testcase_14 AC 81 ms
50,596 KB
testcase_15 AC 91 ms
51,384 KB
testcase_16 AC 75 ms
51,012 KB
testcase_17 AC 98 ms
51,508 KB
testcase_18 AC 81 ms
51,376 KB
testcase_19 AC 78 ms
50,448 KB
testcase_20 AC 77 ms
50,880 KB
testcase_21 AC 80 ms
51,412 KB
testcase_22 AC 90 ms
51,292 KB
testcase_23 AC 83 ms
51,176 KB
testcase_24 AC 900 ms
77,892 KB
testcase_25 AC 1,078 ms
85,032 KB
testcase_26 AC 708 ms
73,968 KB
testcase_27 AC 310 ms
58,396 KB
testcase_28 AC 1,061 ms
83,708 KB
testcase_29 AC 704 ms
79,744 KB
testcase_30 AC 1,139 ms
87,288 KB
testcase_31 AC 469 ms
62,016 KB
testcase_32 AC 501 ms
62,164 KB
testcase_33 AC 657 ms
66,656 KB
testcase_34 AC 648 ms
65,864 KB
testcase_35 AC 965 ms
79,228 KB
testcase_36 AC 513 ms
65,716 KB
testcase_37 AC 672 ms
71,756 KB
testcase_38 AC 927 ms
78,316 KB
testcase_39 AC 540 ms
53,124 KB
testcase_40 AC 584 ms
65,900 KB
testcase_41 AC 1,173 ms
91,372 KB
testcase_42 AC 708 ms
67,772 KB
testcase_43 AC 900 ms
79,332 KB
testcase_44 AC 56 ms
50,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        Thing[] things = new Thing[n];
        for (int i = 0; i < n; i++) {
            things[i] = new Thing(sc.nextInt(), sc.nextInt());
        }
        Arrays.sort(things);
        TreeMap<Integer, Current> lefts = new TreeMap<>();
        long leftCost = 0;
        long leftWeights = 0;
        int prev = Integer.MIN_VALUE;
        lefts.put(Integer.MIN_VALUE, new Current(leftCost, leftWeights));
        for (int i = 0; i < n; i++) {
            leftCost += (things[i].point - prev) * leftWeights;
            leftWeights += things[i].weight;
            lefts.put(things[i].point, new Current(leftCost, leftWeights));
            prev = things[i].point;
        }
        TreeMap<Integer, Current> rights = new TreeMap<>();
        long rightCost = 0;
        long rightWeights = 0;
        prev = Integer.MAX_VALUE;
        rights.put(Integer.MAX_VALUE, new Current(rightCost, rightWeights));
        for (int i = n - 1; i >= 0; i--) {
            rightCost += (prev - things[i].point) * rightWeights;
            rightWeights += things[i].weight;
            rights.put(things[i].point, new Current(rightCost, rightWeights));
            prev = things[i].point;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            int x = sc.nextInt();
            long ans = 0;
            int min = lefts.floorKey(x);
            ans += lefts.get(min).cost + lefts.get(min).weight * (x - min);
            int max = rights.ceilingKey(x);
            ans += rights.get(max).cost + rights.get(max).weight * (max - x);
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static class Thing implements Comparable<Thing> {
        int point;
        int weight;
        
        public Thing(int point, int weight) {
            this.point = point;
            this.weight = weight;
        }
        
        public int compareTo(Thing another) {
            return point - another.point;
        }
    }
    
    static class Current {
        long cost;
        long weight;
        
        public Current(long cost, long weight) {
            this.cost = cost;
            this.weight = weight;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0