結果
| 問題 |
No.1012 荷物収集
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-04-11 08:23:19 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 826 ms / 2,000 ms |
| コード長 | 3,131 bytes |
| コンパイル時間 | 3,361 ms |
| コンパイル使用メモリ | 88,280 KB |
| 実行使用メモリ | 58,916 KB |
| 最終ジャッジ日時 | 2024-10-06 23:39:07 |
| 合計ジャッジ時間 | 23,718 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int q = sc.nextInt();
Point[] points = new Point[n + q];
for (int i = 0; i < n; i++) {
points[i] = new Point(0, sc.nextInt(), sc.nextInt());
}
for (int i = n; i < n + q; i++) {
points[i] = new Point(i - n, sc.nextInt(), 0);
}
Arrays.sort(points);
long[] ans = new long[q];
long leftWeight = 0;
long leftSum = 0;
int leftPrev = 0;
for (int i = 0; i < n + q; i++) {
leftSum += (points[i].position - leftPrev) * leftWeight;
leftWeight += points[i].weight;
leftPrev = points[i].position;
if (points[i].weight == 0) {
ans[points[i].idx] += leftSum;
}
}
long rightWeight = 0;
long rightSum = 0;
int rightPrev = 0;
for (int i = n + q - 1; i >= 0; i--) {
rightSum += (rightPrev - points[i].position) * rightWeight;
rightWeight += points[i].weight;
rightPrev = points[i].position;
if (points[i].weight == 0) {
ans[points[i].idx] += rightSum;
}
}
System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new)));
}
static class Point implements Comparable<Point> {
int idx;
int position;
int weight;
public Point(int idx, int position, int weight) {
this.idx = idx;
this.position = position;
this.weight = weight;
}
public int compareTo(Point another) {
return position - another.position;
}
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
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 int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten