結果
| 問題 |
No.1012 荷物収集
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-24 09:07:48 |
| 言語 | Java (openjdk 23) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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();
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();
}
}
tenten