結果

問題 No.1012 荷物収集
ユーザー tentententen
提出日時 2021-11-24 09:07:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,179 ms / 2,000 ms
コード長 3,115 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 76,392 KB
実行使用メモリ 91,432 KB
最終ジャッジ日時 2023-09-08 15:13:19
合計ジャッジ時間 35,358 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,704 KB
testcase_01 AC 43 ms
49,764 KB
testcase_02 AC 43 ms
49,804 KB
testcase_03 AC 44 ms
49,752 KB
testcase_04 AC 876 ms
91,052 KB
testcase_05 AC 901 ms
91,208 KB
testcase_06 AC 777 ms
89,560 KB
testcase_07 AC 902 ms
91,376 KB
testcase_08 AC 806 ms
89,396 KB
testcase_09 AC 796 ms
90,576 KB
testcase_10 AC 878 ms
91,192 KB
testcase_11 AC 782 ms
91,148 KB
testcase_12 AC 880 ms
91,348 KB
testcase_13 AC 839 ms
91,432 KB
testcase_14 AC 63 ms
50,144 KB
testcase_15 AC 92 ms
52,224 KB
testcase_16 AC 73 ms
49,980 KB
testcase_17 AC 76 ms
51,772 KB
testcase_18 AC 71 ms
51,912 KB
testcase_19 AC 60 ms
51,184 KB
testcase_20 AC 75 ms
50,728 KB
testcase_21 AC 68 ms
50,976 KB
testcase_22 AC 67 ms
51,868 KB
testcase_23 AC 63 ms
50,952 KB
testcase_24 AC 927 ms
78,920 KB
testcase_25 AC 1,125 ms
85,384 KB
testcase_26 AC 714 ms
75,340 KB
testcase_27 AC 288 ms
58,636 KB
testcase_28 AC 1,069 ms
85,388 KB
testcase_29 AC 687 ms
77,844 KB
testcase_30 AC 1,123 ms
91,168 KB
testcase_31 AC 459 ms
62,368 KB
testcase_32 AC 530 ms
64,040 KB
testcase_33 AC 671 ms
67,236 KB
testcase_34 AC 656 ms
67,920 KB
testcase_35 AC 1,074 ms
84,504 KB
testcase_36 AC 501 ms
66,044 KB
testcase_37 AC 692 ms
73,048 KB
testcase_38 AC 939 ms
78,832 KB
testcase_39 AC 525 ms
65,076 KB
testcase_40 AC 588 ms
67,396 KB
testcase_41 AC 1,179 ms
91,396 KB
testcase_42 AC 754 ms
69,476 KB
testcase_43 AC 900 ms
78,440 KB
testcase_44 AC 43 ms
49,832 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