結果
| 問題 | No.1012 荷物収集 |
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-03-24 14:38:25 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,536 ms / 2,000 ms |
| コード長 | 2,176 bytes |
| コンパイル時間 | 2,462 ms |
| コンパイル使用メモリ | 78,948 KB |
| 実行使用メモリ | 78,928 KB |
| 最終ジャッジ日時 | 2024-12-31 15:00:06 |
| 合計ジャッジ時間 | 45,663 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
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;
}
}
}
htensai