結果

問題 No.1012 荷物収集
ユーザー tentententen
提出日時 2021-08-06 15:10:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 680 ms / 2,000 ms
コード長 2,427 bytes
コンパイル時間 2,983 ms
コンパイル使用メモリ 78,628 KB
実行使用メモリ 71,584 KB
最終ジャッジ日時 2023-10-17 01:54:55
合計ジャッジ時間 21,394 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
51,528 KB
testcase_01 AC 52 ms
53,548 KB
testcase_02 AC 52 ms
53,540 KB
testcase_03 AC 53 ms
53,548 KB
testcase_04 AC 449 ms
70,720 KB
testcase_05 AC 430 ms
69,984 KB
testcase_06 AC 443 ms
70,988 KB
testcase_07 AC 447 ms
71,128 KB
testcase_08 AC 437 ms
69,912 KB
testcase_09 AC 438 ms
71,260 KB
testcase_10 AC 440 ms
71,168 KB
testcase_11 AC 441 ms
71,584 KB
testcase_12 AC 430 ms
69,200 KB
testcase_13 AC 453 ms
71,252 KB
testcase_14 AC 61 ms
53,572 KB
testcase_15 AC 76 ms
54,412 KB
testcase_16 AC 75 ms
53,616 KB
testcase_17 AC 84 ms
54,408 KB
testcase_18 AC 80 ms
54,404 KB
testcase_19 AC 59 ms
53,576 KB
testcase_20 AC 67 ms
54,132 KB
testcase_21 AC 68 ms
54,440 KB
testcase_22 AC 68 ms
54,156 KB
testcase_23 AC 61 ms
54,148 KB
testcase_24 AC 499 ms
66,992 KB
testcase_25 AC 647 ms
70,908 KB
testcase_26 AC 408 ms
62,600 KB
testcase_27 AC 220 ms
58,524 KB
testcase_28 AC 612 ms
68,792 KB
testcase_29 AC 421 ms
65,704 KB
testcase_30 AC 632 ms
70,772 KB
testcase_31 AC 388 ms
65,144 KB
testcase_32 AC 339 ms
63,012 KB
testcase_33 AC 412 ms
69,028 KB
testcase_34 AC 443 ms
67,364 KB
testcase_35 AC 555 ms
67,216 KB
testcase_36 AC 442 ms
67,016 KB
testcase_37 AC 411 ms
63,024 KB
testcase_38 AC 531 ms
67,740 KB
testcase_39 AC 332 ms
62,820 KB
testcase_40 AC 358 ms
62,832 KB
testcase_41 AC 680 ms
69,596 KB
testcase_42 AC 402 ms
65,228 KB
testcase_43 AC 502 ms
65,412 KB
testcase_44 AC 55 ms
53,532 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();
        Pack[] packs = new Pack[n];
        long ans = 0;
        int right = 0;
        for (int i = 0; i < n; i++) {
            packs[i] = new Pack(sc.nextInt(), sc.nextInt());
            ans += (long)packs[i].weight * packs[i].position;
            right += packs[i].weight;
        }
        Arrays.sort(packs);
        Pack[] places = new Pack[q];
        for (int i = 0; i < q; i++) {
            places[i] = new Pack(sc.nextInt(), i);
        }
        Arrays.sort(places);
        int left = 0;
        int prev = 0;
        int idx = 0;
        long[] results = new long[q];
        for (int i = 0; i < q; i++) {
            int x = places[i].position;
            ans += (long)left * (x - prev);
            while (idx < n && packs[idx].position <= x) {
                ans += ((long)x + prev - packs[idx].position * 2) * packs[idx].weight;
                left += packs[idx].weight;
                right -= packs[idx].weight;
                idx++;
            }
            ans -= (long)right * (x - prev);
            results[places[i].weight] = ans;
            prev = x;
        }
        StringBuilder sb = new StringBuilder();
        for (long x : results) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
    
    static class Pack implements Comparable<Pack> {
        int position;
        int weight;
        
        public Pack(int position, int weight) {
            this.position = position;
            this.weight = weight;
        }
        
        public int compareTo(Pack another) {
            return position - another.position;
        }
    }
    

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