0%

Acwing 1913 公平摄影(思维、前缀和、哈希)

题目链接

打卡每日一练, 今天才学会Java对象数组排序方法……

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
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.HashMap;

public class Main {
public static class Node {
public int dis;
public int val;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Node[] a = new Node[n+1];
for(int i = 0; i <= n; i++) a[i] = new Node();
for(int i = 1; i <= n; i++) {
a[i].dis = in.nextInt();
String tmp = in.next();
if(tmp.charAt(0) == 'G') a[i].val = 1;
else a[i].val = -1;
}
Comparator<Node> comp = new Comparator<Main.Node>() {
public int compare(Node a, Node b) {
if(a.dis > b.dis) return 1;
else if(a.dis < b.dis) return -1;
else return 0;
}
};
Arrays.sort(a, 1, n + 1, comp);
Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
int res = 0, sum = 0, last = 0;
for(int i = 1; i <= n; i++) {
if(mp.get(sum) == null) {
mp.put(sum, a[i].dis);
}
sum += a[i].val;
if(mp.get(sum) != null) {
res = Math.max(res, a[i].dis - mp.get(sum));
}
if(i == 1 || a[i].val != a[i-1].val) last = a[i].dis;
res = Math.max(res, a[i].dis - last);
}
System.out.println(res);

}
}

求大佬赏个饭