#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 201010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
string str[MAX];
ll K[MAX];
int fs[MAX];
int ms[MAX];
struct node {
ll sum = 0;
ll lmx = 0;
node(int x = 0) { sum = lmx = x; }
};
node operator+(node n1, node n2) {
node ret;
ret.sum = n1.sum + n2.sum;
ret.lmx = max(n1.lmx, n1.sum + n2.lmx);
return ret;
}
node operator*(node n1, ll x) {
if (!x) return node();
node res = n1 * (x / 2);
res = res + res;
if (x & 1) res = n1 + res;
return res;
}
node gets(int s, int n = -1) {
if (!~n) n = str[s].size();
int i;
node ret;
for (i = 0; i < n; i++) {
if (str[s][i] == 'M') ret = ret + node(-1);
else ret = ret + node(1);
}
return ret;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
ll N;
int M;
cin >> N >> M;
int i, j;
ll allf = 0;
for (i = 1; i <= M; i++) {
cin >> str[i] >> K[i];
for (auto v : str[i]) if (v == 'F') fs[i]++;
ms[i] = str[i].size() - fs[i];
allf += 1ll * fs[i] * K[i];
}
if (allf < N) {
cout << -1 << ln;
return 0;
}
ll lim = (allf - N) * 2 + 1;
ll l, r;
l = -1;
r = N;
while (r - l > 1) {
ll m = l + r >> 1;
ll rem = m;
node res = node();
int isend = 0;
for (i = M; i >= 1; i--) {
if (isend) {
res = (gets(i) * K[i]) + res;
continue;
}
if (rem >= 1ll * ms[i] * K[i]) {
rem -= 1ll * ms[i] * K[i];
res = res + node(fs[i] * K[i]);
}
else {
ll n = rem / ms[i];
rem -= 1ll * ms[i] * n;
res = res + node(fs[i] * n);
int sfs = 0;
for (j = str[i].size() - 1; j >= 0; j--) {
if (!rem) break;
if (str[i][j] == 'F') sfs++;
else rem--;
}
res = gets(i, j) + node(sfs) + res;
res = gets(i) * (K[i] - n - 1) + res;
isend = 1;
}
}
res = node(-m) + res;
if (res.lmx > lim) l = m;
else r = m;
}
cout << r << ln;
}
Compilation message
toilets.cpp: In function 'int main()':
toilets.cpp:70:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | ll m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6628 KB |
Output is correct |
2 |
Correct |
4 ms |
6612 KB |
Output is correct |
3 |
Correct |
4 ms |
6612 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
3 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
4 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6612 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6632 KB |
Output is correct |
20 |
Correct |
4 ms |
6612 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6628 KB |
Output is correct |
2 |
Correct |
4 ms |
6612 KB |
Output is correct |
3 |
Correct |
4 ms |
6612 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
3 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
4 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6612 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6632 KB |
Output is correct |
20 |
Correct |
4 ms |
6612 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
23 |
Correct |
47 ms |
6880 KB |
Output is correct |
24 |
Correct |
65 ms |
6996 KB |
Output is correct |
25 |
Correct |
60 ms |
6988 KB |
Output is correct |
26 |
Correct |
22 ms |
6996 KB |
Output is correct |
27 |
Correct |
4 ms |
6996 KB |
Output is correct |
28 |
Correct |
59 ms |
6996 KB |
Output is correct |
29 |
Correct |
56 ms |
6996 KB |
Output is correct |
30 |
Correct |
47 ms |
6880 KB |
Output is correct |
31 |
Correct |
59 ms |
6880 KB |
Output is correct |
32 |
Correct |
60 ms |
6996 KB |
Output is correct |
33 |
Correct |
4 ms |
6880 KB |
Output is correct |
34 |
Correct |
33 ms |
6996 KB |
Output is correct |
35 |
Correct |
66 ms |
6996 KB |
Output is correct |
36 |
Correct |
66 ms |
6996 KB |
Output is correct |
37 |
Correct |
21 ms |
6996 KB |
Output is correct |
38 |
Correct |
5 ms |
6996 KB |
Output is correct |
39 |
Correct |
51 ms |
6880 KB |
Output is correct |
40 |
Correct |
50 ms |
6996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6628 KB |
Output is correct |
2 |
Correct |
4 ms |
6612 KB |
Output is correct |
3 |
Correct |
4 ms |
6612 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
3 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
4 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6612 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6632 KB |
Output is correct |
20 |
Correct |
4 ms |
6612 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
23 |
Correct |
47 ms |
6880 KB |
Output is correct |
24 |
Correct |
65 ms |
6996 KB |
Output is correct |
25 |
Correct |
60 ms |
6988 KB |
Output is correct |
26 |
Correct |
22 ms |
6996 KB |
Output is correct |
27 |
Correct |
4 ms |
6996 KB |
Output is correct |
28 |
Correct |
59 ms |
6996 KB |
Output is correct |
29 |
Correct |
56 ms |
6996 KB |
Output is correct |
30 |
Correct |
47 ms |
6880 KB |
Output is correct |
31 |
Correct |
59 ms |
6880 KB |
Output is correct |
32 |
Correct |
60 ms |
6996 KB |
Output is correct |
33 |
Correct |
4 ms |
6880 KB |
Output is correct |
34 |
Correct |
33 ms |
6996 KB |
Output is correct |
35 |
Correct |
66 ms |
6996 KB |
Output is correct |
36 |
Correct |
66 ms |
6996 KB |
Output is correct |
37 |
Correct |
21 ms |
6996 KB |
Output is correct |
38 |
Correct |
5 ms |
6996 KB |
Output is correct |
39 |
Correct |
51 ms |
6880 KB |
Output is correct |
40 |
Correct |
50 ms |
6996 KB |
Output is correct |
41 |
Correct |
23 ms |
8148 KB |
Output is correct |
42 |
Incorrect |
212 ms |
8184 KB |
Output isn't correct |
43 |
Halted |
0 ms |
0 KB |
- |