-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathCodeforces_0978C_Letters.java
More file actions
36 lines (34 loc) · 1.16 KB
/
Codeforces_0978C_Letters.java
File metadata and controls
36 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// AC: 1687 ms
// 16700 KB
// Binary search, Using TreeMap.floorKey()
// T:O(m * logn), S:O(n)
//
import java.util.Scanner;
import java.util.TreeMap;
public class Codeforces_0978C_Letters {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
long sum = 0;
long[] dormitoryRoomCount = new long[n];
TreeMap<Long, Integer> numToDormitroy = new TreeMap<>();
numToDormitroy.put(0L, 0);
for (int i = 0; i < n; i++) {
long a = sc.nextLong();
sum += a;
dormitoryRoomCount[i] = a;
numToDormitroy.put(sum, i + 1);
}
for (int i = 0; i < m; i++) {
long b = sc.nextLong();
long dormitoryKey = numToDormitroy.floorKey(b), roomNum = b - dormitoryKey;
int dormitoryNum = numToDormitroy.get(dormitoryKey);
if (b == dormitoryKey) {
roomNum = dormitoryRoomCount[(int) (dormitoryNum - 1)];
} else {
dormitoryNum = dormitoryNum + 1;
}
System.out.println(dormitoryNum + " " + roomNum);
}
}
}