forked from namanj401/scaling-enigma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
F2.java
187 lines (186 loc) · 4.19 KB
/
F2.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class gglks2021g1
{
static void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
static void sort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
}
}
public static int lowerBound(int[] array, int length, int value)
{
int low = 0;
int high = length;
while (low < high)
{
final int mid = (low + high) / 2;
//checks if the value is less than middle element of the array
if (value <= array[mid])
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
public static int upperBound(int[] array, int length, int value)
{
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= array[mid]) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long n,long m)
{
if(m==0)
return 1;
long ans=1;
while(m>0)
{
ans=ans*n;
m--;
}
return ans;
}
static long[][] arrSorter(long arr[][])
{
int n=arr.length;
for(int i=0;i<n;)
{
int j=i;
ArrayList<Long> arrli=new ArrayList<Long>();
while(j<n&&arr[j][0]==arr[i][0])
{
arrli.add(arr[j][1]);
j++;
}
Collections.sort(arrli);
for(int k=i;k<j;k++)
{
arr[k][1]=arrli.get(k-i);
}
}
return arr;
}
static int BinarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
out:for(int i=1;i<=t;i++)
{
String s=br.readLine();
String str[]=s.split(" ");
int n=Integer.parseInt(str[0]);
long d=Integer.parseInt(str[1]);
long c=Integer.parseInt(str[2]);
long m=Integer.parseInt(str[3]);
s=br.readLine();
int pos=-1;
for(int j=0;j<n;j++)
{
if(s.charAt(j)=='D'&&d>0)
{
d--;
c+=m;
}
else if(s.charAt(j)=='C'&&c>0)
c--;
else
{
pos=j;
break;
}
}
if(pos==-1)
System.out.println("Case #"+i+": YES");
else
{
for(int j=pos;j<n;j++)
{
if(s.charAt(j)=='D')
{
System.out.println("Case #"+i+": NO");
continue out;
}
}
System.out.println("Case #"+i+": YES");
}
}
}
}