-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1244B-Rooms and Staircases.java
30 lines (24 loc) · 1.13 KB
/
1244B-Rooms and Staircases.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
package codeforces;
import java.util.Scanner;
public class roomsstaircases {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt(); // Number of test cases
while (t-- > 0) {
int n = scanner.nextInt(); // Number of rooms on each floor
String s = scanner.next(); // String representing staircases
// If there are no staircases, the maximum rooms Nikolay can visit is just the rooms on one floor
if (!s.contains("1")) {
System.out.println(n);
continue;
}
// Find the first and last occurrences of '1'
int firstStaircase = s.indexOf('1');
int lastStaircase = s.lastIndexOf('1');
// The maximum rooms Nikolay can visit is determined by the farthest stretch that includes both floors
// He can visit rooms from the first to the last staircase, and both floors in that segment.
int maxRooms = Math.max(lastStaircase + 1, n - firstStaircase) * 2;
System.out.println(maxRooms);
}
}
}