File tree Expand file tree Collapse file tree 1 file changed +73
-0
lines changed
Expand file tree Collapse file tree 1 file changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st = new StringTokenizer (" " );
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static String nextToken() throws Exception {
15+ while (! st. hasMoreTokens()) nextLine();
16+ return st. nextToken();
17+ }
18+ static int nextInt() throws Exception { return Integer . parseInt(nextToken()); }
19+ static long nextLong() throws Exception { return Long . parseLong(nextToken()); }
20+ static double nextDouble() throws Exception { return Double . parseDouble(nextToken()); }
21+ static void bwEnd() throws Exception {bw. flush();bw. close();}
22+
23+ // Additional field
24+
25+ static int N ;
26+ static PriorityQueue<int[]> Q = new PriorityQueue<> ((a,b) - > {
27+ if (a[0 ] == b[0 ]) return b[1 ]- a[1 ];
28+ return a[0 ]- b[0 ];
29+ });
30+
31+ public static void main(String [] args) throws Exception {
32+
33+ ready();
34+ solve();
35+
36+ bwEnd();
37+
38+ }
39+
40+ static void ready() throws Exception {
41+
42+ N = nextInt();
43+ int cur = 0 ;
44+ for (int i= 0 ;i< N ;i++ ) {
45+ int len = nextInt();
46+ char a = nextToken(). charAt(0 );
47+ if (a == ' L' ) len *= - 1 ;
48+ int next = cur + len;
49+ Q . offer(new int [] {Math . min(cur, next),1 });
50+ Q . offer(new int [] {Math . max(cur, next),- 1 });
51+ cur = next;
52+ }
53+
54+ }
55+
56+ static void solve() throws Exception {
57+
58+ int ans = 0 , cnt = 0 ;
59+
60+ int prev = 0 ;
61+ while (! Q . isEmpty()) {
62+ int now = Q . peek()[0 ];
63+ if (cnt > 1 ) ans += now - prev;
64+ while (! Q . isEmpty() && Q . peek()[0 ] == now) cnt += Q . poll()[1 ];
65+ prev = now;
66+ }
67+ bw. write(ans + " \n " );
68+
69+ }
70+
71+ }
72+
73+ ```
You can’t perform that action at this time.
0 commit comments