#include <bits/stdc++.h>
using namespace std;
long long K[100009], A[100009], MC[100009];
string S[100009];
int main() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(0);
long long N; int M; cin >> N >> M;
for(int i=0; i<M; i++) {
cin >> S[i] >> K[i];
for(auto& it: S[i]) {
if(it == 'F') ++A[i];
else --A[i], ++MC[i];
}
}
long long l = 0, r = 2LL*N;
while(l <= r) {
bool fl = 1;
long long m = l+r >> 1, s = m, c = 0;
for(int i=M-1; i>=0; i--) {
if(s <= 0LL) {
for(int j=(int)S[i].size()-1; j>=0; j--) {
if(S[i][j] == 'M') --c;
if(S[i][j] == 'F') ++c;
if(c + m < -1LL) fl = 0;
}
if(K[i] >= 2LL) {
c += (K[i] - 2) * A[i];
for(int j=(int)S[i].size()-1; j>=0; j--) {
if(S[i][j] == 'M') --c;
if(S[i][j] == 'F') ++c;
if(c + m < -1LL) fl = 0;
}
}
}
else if(s - K[i] * MC[i] <= 0LL) {
for(int j=(int)S[i].size()-1; j>=0; j--) {
if(s > 0LL && S[i][j] == 'M') --s, --c;
else if(S[i][j] == 'M') --c;
if(S[i][j] == 'F') ++c;
if(c + min(m - s, m) < -1LL) fl = 0;
}
if(K[i] >= 2LL) {
s -= (K[i] - 2) * MC[i]; c += (K[i] - 2) * A[i];
for(int j=(int)S[i].size()-1; j>=0; j--) {
if(s > 0LL && S[i][j] == 'M') --s, --c;
else if(S[i][j] == 'M') --c;
if(S[i][j] == 'F') ++c;
if(c + min(m - s, m) < -1LL) fl = 0;
}
}
s = 0;
}
else c += K[i] * A[i], s -= K[i] * MC[i];
}
if(c < -1LL) fl = 0;
if(fl) r = m-1;
else l = m+1;
}
if(r == 2LL*N) cout << -1;
else cout << r+1;
return 0;
}
Compilation message
toilets.cpp: In function 'int main()':
toilets.cpp:21:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
long long m = l+r >> 1, s = m, c = 0;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
5 ms |
3456 KB |
Output is correct |
5 |
Correct |
5 ms |
3456 KB |
Output is correct |
6 |
Correct |
4 ms |
3560 KB |
Output is correct |
7 |
Correct |
4 ms |
3456 KB |
Output is correct |
8 |
Correct |
5 ms |
3456 KB |
Output is correct |
9 |
Correct |
4 ms |
3456 KB |
Output is correct |
10 |
Correct |
5 ms |
3456 KB |
Output is correct |
11 |
Correct |
4 ms |
3456 KB |
Output is correct |
12 |
Correct |
5 ms |
3456 KB |
Output is correct |
13 |
Correct |
5 ms |
3456 KB |
Output is correct |
14 |
Correct |
5 ms |
3456 KB |
Output is correct |
15 |
Correct |
5 ms |
3456 KB |
Output is correct |
16 |
Correct |
4 ms |
3568 KB |
Output is correct |
17 |
Correct |
5 ms |
3456 KB |
Output is correct |
18 |
Correct |
4 ms |
3584 KB |
Output is correct |
19 |
Correct |
4 ms |
3456 KB |
Output is correct |
20 |
Correct |
4 ms |
3456 KB |
Output is correct |
21 |
Correct |
4 ms |
3456 KB |
Output is correct |
22 |
Correct |
5 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
5 ms |
3456 KB |
Output is correct |
5 |
Correct |
5 ms |
3456 KB |
Output is correct |
6 |
Correct |
4 ms |
3560 KB |
Output is correct |
7 |
Correct |
4 ms |
3456 KB |
Output is correct |
8 |
Correct |
5 ms |
3456 KB |
Output is correct |
9 |
Correct |
4 ms |
3456 KB |
Output is correct |
10 |
Correct |
5 ms |
3456 KB |
Output is correct |
11 |
Correct |
4 ms |
3456 KB |
Output is correct |
12 |
Correct |
5 ms |
3456 KB |
Output is correct |
13 |
Correct |
5 ms |
3456 KB |
Output is correct |
14 |
Correct |
5 ms |
3456 KB |
Output is correct |
15 |
Correct |
5 ms |
3456 KB |
Output is correct |
16 |
Correct |
4 ms |
3568 KB |
Output is correct |
17 |
Correct |
5 ms |
3456 KB |
Output is correct |
18 |
Correct |
4 ms |
3584 KB |
Output is correct |
19 |
Correct |
4 ms |
3456 KB |
Output is correct |
20 |
Correct |
4 ms |
3456 KB |
Output is correct |
21 |
Correct |
4 ms |
3456 KB |
Output is correct |
22 |
Correct |
5 ms |
3456 KB |
Output is correct |
23 |
Correct |
21 ms |
4096 KB |
Output is correct |
24 |
Correct |
25 ms |
4096 KB |
Output is correct |
25 |
Correct |
23 ms |
4096 KB |
Output is correct |
26 |
Correct |
12 ms |
4096 KB |
Output is correct |
27 |
Correct |
7 ms |
4096 KB |
Output is correct |
28 |
Correct |
18 ms |
4096 KB |
Output is correct |
29 |
Correct |
22 ms |
4096 KB |
Output is correct |
30 |
Correct |
20 ms |
3980 KB |
Output is correct |
31 |
Correct |
23 ms |
4096 KB |
Output is correct |
32 |
Correct |
25 ms |
4096 KB |
Output is correct |
33 |
Correct |
8 ms |
4096 KB |
Output is correct |
34 |
Correct |
15 ms |
4096 KB |
Output is correct |
35 |
Correct |
26 ms |
4100 KB |
Output is correct |
36 |
Correct |
27 ms |
4096 KB |
Output is correct |
37 |
Correct |
11 ms |
4096 KB |
Output is correct |
38 |
Correct |
7 ms |
4096 KB |
Output is correct |
39 |
Correct |
20 ms |
4096 KB |
Output is correct |
40 |
Correct |
21 ms |
4096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3456 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
5 ms |
3456 KB |
Output is correct |
5 |
Correct |
5 ms |
3456 KB |
Output is correct |
6 |
Correct |
4 ms |
3560 KB |
Output is correct |
7 |
Correct |
4 ms |
3456 KB |
Output is correct |
8 |
Correct |
5 ms |
3456 KB |
Output is correct |
9 |
Correct |
4 ms |
3456 KB |
Output is correct |
10 |
Correct |
5 ms |
3456 KB |
Output is correct |
11 |
Correct |
4 ms |
3456 KB |
Output is correct |
12 |
Correct |
5 ms |
3456 KB |
Output is correct |
13 |
Correct |
5 ms |
3456 KB |
Output is correct |
14 |
Correct |
5 ms |
3456 KB |
Output is correct |
15 |
Correct |
5 ms |
3456 KB |
Output is correct |
16 |
Correct |
4 ms |
3568 KB |
Output is correct |
17 |
Correct |
5 ms |
3456 KB |
Output is correct |
18 |
Correct |
4 ms |
3584 KB |
Output is correct |
19 |
Correct |
4 ms |
3456 KB |
Output is correct |
20 |
Correct |
4 ms |
3456 KB |
Output is correct |
21 |
Correct |
4 ms |
3456 KB |
Output is correct |
22 |
Correct |
5 ms |
3456 KB |
Output is correct |
23 |
Correct |
21 ms |
4096 KB |
Output is correct |
24 |
Correct |
25 ms |
4096 KB |
Output is correct |
25 |
Correct |
23 ms |
4096 KB |
Output is correct |
26 |
Correct |
12 ms |
4096 KB |
Output is correct |
27 |
Correct |
7 ms |
4096 KB |
Output is correct |
28 |
Correct |
18 ms |
4096 KB |
Output is correct |
29 |
Correct |
22 ms |
4096 KB |
Output is correct |
30 |
Correct |
20 ms |
3980 KB |
Output is correct |
31 |
Correct |
23 ms |
4096 KB |
Output is correct |
32 |
Correct |
25 ms |
4096 KB |
Output is correct |
33 |
Correct |
8 ms |
4096 KB |
Output is correct |
34 |
Correct |
15 ms |
4096 KB |
Output is correct |
35 |
Correct |
26 ms |
4100 KB |
Output is correct |
36 |
Correct |
27 ms |
4096 KB |
Output is correct |
37 |
Correct |
11 ms |
4096 KB |
Output is correct |
38 |
Correct |
7 ms |
4096 KB |
Output is correct |
39 |
Correct |
20 ms |
4096 KB |
Output is correct |
40 |
Correct |
21 ms |
4096 KB |
Output is correct |
41 |
Correct |
39 ms |
7388 KB |
Output is correct |
42 |
Correct |
61 ms |
7480 KB |
Output is correct |
43 |
Correct |
5 ms |
3456 KB |
Output is correct |
44 |
Correct |
4 ms |
3456 KB |
Output is correct |
45 |
Correct |
5 ms |
3456 KB |
Output is correct |
46 |
Correct |
121 ms |
4352 KB |
Output is correct |
47 |
Correct |
112 ms |
3968 KB |
Output is correct |
48 |
Correct |
110 ms |
3968 KB |
Output is correct |
49 |
Correct |
72 ms |
3984 KB |
Output is correct |
50 |
Correct |
46 ms |
3976 KB |
Output is correct |
51 |
Correct |
131 ms |
4444 KB |
Output is correct |
52 |
Correct |
57 ms |
7032 KB |
Output is correct |
53 |
Correct |
56 ms |
7032 KB |
Output is correct |
54 |
Correct |
62 ms |
6912 KB |
Output is correct |
55 |
Correct |
41 ms |
3968 KB |
Output is correct |
56 |
Correct |
48 ms |
3840 KB |
Output is correct |