-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA02346.java
43 lines (39 loc) · 1.38 KB
/
A02346.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
package acmicpc;
import java.io.*;
public class A02346 {
public static void solution(int[] data) {
StringBuilder sb = new StringBuilder();
int len = data.length, remaining = data.length - 1, nxt = data[0];
int cur = 0, dir = nxt > 0 ? 1 : -1;
int[] popped = new int[len];
popped[cur] = 1;
sb.append(cur + 1 + " ");
while (remaining > 0) {
int moveCount = 0;
while (moveCount < Math.abs(nxt)) {
cur += dir;
cur = cur == len ? 0 : cur < 0 ? len - 1 : cur;
moveCount += popped[cur] == 0 ? 1 : 0;
}
popped[cur] = 1;
sb.append(cur + 1 + " ");
nxt = data[cur];
dir = nxt > 0 ? 1 : -1;
remaining--;
}
System.out.println(sb);
}
public void reader() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int len = Integer.parseInt(br.readLine());
int[] balloon = new int[len];
String[] ball = br.readLine().split(" ");
for (int i = 0; i < len; i++)
balloon[i] = Integer.parseInt(ball[i]);
solution(balloon);
}
public static void main(String[] args) throws NumberFormatException, IOException {
A02346 test = new A02346();
test.reader();
}
}