-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_prob1.java
More file actions
69 lines (61 loc) · 1.81 KB
/
random_prob1.java
File metadata and controls
69 lines (61 loc) · 1.81 KB
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
public static Solution{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
int n=Integer.parseInt(scan.nextLine().trim());
String s =scan.nextLine();
int B=Integer.parseInt(scan.nextLine().trim());
int G=Integer.parseInt(scan.nextLine().trim());
int result = fillseats(n,s,B,G);
System.out.println(result);
}
public static int fillseat(int n, String s, int b, int g){
ArrayList<Integer>arr = new ArrayList<>();
int count = 0;
//ArrayList construct
for(int i=0;i<n;i++){
char ch=s.charAt(i);
if(ch=='0'){
count++;
}
else{
if(count!=0)
arr.add(count);
count = 0;
}
}
//seating
n=arr.size();
int total=0;
for(int i=0;i<n;i++){
int val = arr.get(i);
int seat = val/2;
if(g>=seat && boy >=seat){
g-=seat;boy-=seat;
if(val%2!=0){
if(g>b)
g--;
else
b--;
}
total+=val;
}
else{
if(g<seat && b<seat && b==g){
total + = g*2;
g=0;
b=0;
}
else{
int min=Math.min(g,b);
total+=min*2+1;
g-=min;
b-=min;
if(g>b)
g--;
else
b--;
}
}
}
}
}