File tree Expand file tree Collapse file tree 1 file changed +75
-0
lines changed
Expand file tree Collapse file tree 1 file changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
9+ StringTokenizer st = new StringTokenizer (br. readLine());
10+
11+ int N = Integer . parseInt(st. nextToken());
12+ int K = Integer . parseInt(st. nextToken());
13+
14+ int [] belt = new int [2 * N ];
15+ boolean [] robot = new boolean [2 * N ];
16+
17+ st = new StringTokenizer (br. readLine());
18+ for (int i = 0 ; i < 2 * N ; i++ ) {
19+ belt[i] = Integer . parseInt(st. nextToken());
20+ }
21+
22+ int step = 0 ;
23+
24+ while (true ) {
25+ step++ ;
26+
27+ int tempBelt = belt[2 * N - 1 ];
28+ boolean tempRobot = robot[2 * N - 1 ];
29+
30+ for (int i = 2 * N - 1 ; i > 0 ; i-- ) {
31+ belt[i] = belt[i - 1 ];
32+ robot[i] = robot[i - 1 ];
33+ }
34+
35+ belt[0 ] = tempBelt;
36+ robot[0 ] = tempRobot;
37+
38+ if (robot[N - 1 ]) {
39+ robot[N - 1 ] = false ;
40+ }
41+
42+ for (int i = N - 2 ; i >= 0 ; i-- ) {
43+ if (robot[i] && ! robot[i + 1 ] && belt[i + 1 ] > 0 ) {
44+ robot[i] = false ;
45+ robot[i + 1 ] = true ;
46+ belt[i + 1 ]-- ;
47+
48+ if (i + 1 == N - 1 ) {
49+ robot[i + 1 ] = false ;
50+ }
51+ }
52+ }
53+
54+ if (! robot[0 ] && belt[0 ] > 0 ) {
55+ robot[0 ] = true ;
56+ belt[0 ]-- ;
57+ }
58+
59+ int zeroCount = 0 ;
60+ for (int i = 0 ; i < 2 * N ; i++ ) {
61+ if (belt[i] == 0 ) {
62+ zeroCount++ ;
63+ }
64+ }
65+
66+ if (zeroCount >= K ) {
67+ break ;
68+ }
69+ }
70+
71+ bw. write(step+ " " );
72+ bw. close();
73+ }
74+ }
75+ ```
You can’t perform that action at this time.
0 commit comments