結果
問題 |
No.1012 荷物収集
|
ユーザー |
![]() |
提出日時 | 2021-08-06 15:10:26 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 708 ms / 2,000 ms |
コード長 | 2,427 bytes |
コンパイル時間 | 2,530 ms |
コンパイル使用メモリ | 78,644 KB |
実行使用メモリ | 58,572 KB |
最終ジャッジ日時 | 2024-09-17 00:29:18 |
合計ジャッジ時間 | 21,612 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
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(); } }