-
Notifications
You must be signed in to change notification settings - Fork 0
/
3089.java
115 lines (90 loc) · 3.32 KB
/
3089.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Main {
static Map<Integer, List<Integer>> row = new HashMap<>();
static Map<Integer, List<Integer>> col = new HashMap<>();
static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
// 초기화
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
if (!row.containsKey(x)) {
ArrayList<Integer> list = new ArrayList<>();
list.add(y);
row.put(x, list);
} else {
List<Integer> list = row.get(x);
list.add(y);
}
if (!col.containsKey(y)) {
ArrayList<Integer> list = new ArrayList<>();
list.add(x);
col.put(y, list);
} else {
List<Integer> list = col.get(y);
list.add(x);
}
}
st = new StringTokenizer(br.readLine());
String str = st.nextToken();
int x = 0;
int y = 0;
for (Map.Entry<Integer, List<Integer>> entry : row.entrySet()) {
List<Integer> list = entry.getValue();
list.sort(Comparator.naturalOrder());
}
for (Map.Entry<Integer, List<Integer>> entry : col.entrySet()) {
List<Integer> list = entry.getValue();
list.sort(Comparator.naturalOrder());
}
for (int i = 0; i < M; i++) {
char ch = str.charAt(i);
if (ch == 'U') {
List<Integer> list = row.get(x);
int value = binarySearch(y, list);
y = list.get(value + 1);
} else if (ch == 'D') {
List<Integer> list = row.get(x);
int value = binarySearch(y, list);
y = list.get(value - 1);
} else if (ch == 'R') {
List<Integer> list = col.get(y);
int value = binarySearch(x, list);
x = list.get(value + 1);
} else {
List<Integer> list = col.get(y);
int value = binarySearch(x, list);
x = list.get(value - 1);
}
//System.out.println("x + \" \" + y = " + x + " " + y);
}
bw.write(x + " " + y + "\n");
bw.flush();
}
public static int binarySearch(int target, List<Integer> list) {
int start = 0;
int end = list.size() - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (list.get(mid) < target) {
start = mid + 1;
} else if (list.get(mid) == target) {
return mid;
} else {
end = mid - 1;
}
}
return 0;
}
}