#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 100010;
ll N, msum;
int M;
string S[MN];
ll K[MN];
vector<string> T;
vector<ll> X;
void rem(char c, char o, ll x) {
vector<string> tt = T;
vector<ll> xx = X;
T.clear();
X.clear();
for(int i = (int)tt.size() - 1; i >= 0; i--) {
int cnt = 0;
for(int j = 0; j < tt[i].size(); j++) if(tt[i][j] == c) cnt++;
if(x) {
if(x >= xx[i] * cnt) {
if((int)tt[i].size() - cnt) {
T.push_back("");
T.back().push_back(o);
X.push_back(xx[i] * ((int)tt[i].size() - cnt));
}
x -= xx[i] * cnt;
}
else {
ll q = x / cnt;
if(q && (int)tt[i].size() - cnt) {
T.push_back("");
T.back().push_back(o);
X.push_back(q * ((int)tt[i].size() - cnt));
}
x -= q * cnt;
xx[i] -= q;
string tmp;
cnt = 0;
for(int j = (int)tt[i].size() - 1; j >= 0; j--) {
if(tt[i][j] == c) {
if(cnt >= x) tmp.push_back(c);
cnt++;
}
else tmp.push_back(o);
}
reverse(tmp.begin(), tmp.end());
T.push_back(tmp);
X.push_back(1);
if(xx[i] > 1) {
T.push_back(tt[i]);
X.push_back(xx[i] - 1);
}
x = 0;
}
}
else {
T.push_back(tt[i]);
X.push_back(xx[i]);
}
}
reverse(T.begin(), T.end());
reverse(X.begin(), X.end());
}
void rev() {
for(int i = 0; i < T.size(); i++) {
reverse(T[i].begin(), T[i].end());
}
reverse(T.begin(), T.end());
reverse(X.begin(), X.end());
}
void print() {
for(int i = 0; i < T.size(); i++) {
for(int j = 0; j < X[i]; j++) {
cout << T[i];
}
cout << ' ';
}
cout << endl;
}
bool check(ll x) {
//cout << x << endl;
T.clear();
X.clear();
if(x) {
T.push_back("M");
X.push_back(x);
}
for(int i = 0; i < M; i++) {
T.push_back(S[i]);
X.push_back(K[i]);
}
rem('M', 'F', x);
//print();
rev();
rem('F', 'M', 2 * N - 2 * msum);
rev();
//print();
ll sum = 0;
ll cnt = 0;
for(int i = 0; i < T.size(); i++) {
ll a = 0;
ll b = 0;
ll c = 1e18;
for(int j = 0; j < T[i].size(); j++) {
if(T[i][j] == 'M') a++;
else a--;
if(j % 2) b = min(b, a);
else c = min(c, a);
}
if(a >= 0) {
if(sum + (cnt % 2? c : b) < 0) return false;
if(X[i] > 1 && sum + a + ((cnt + (int)T[i].size()) % 2? c : b) < 0) return false;
sum += a * X[i];
cnt += X[i] * (int)T[i].size();
}
else {
if(sum + a * (X[i] - 1) + ((cnt + (X[i] - 1) * (int)T[i].size()) % 2? c : b) < 0) return false;
if(X[i] > 1 && sum + a * (X[i] - 2) + ((cnt + (X[i] - 2) * (int)T[i].size()) % 2? c : b) < 0) return false;
sum += a * X[i];
cnt += X[i] * (int)T[i].size();
}
}
return true;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> N >> M;
for(int i = 0; i < M; i++) {
cin >> S[i] >> K[i];
int cnt = 0;
for(int j = 0; j < S[i].size(); j++) {
if(S[i][j] == 'M') cnt++;
}
msum += K[i] * cnt;
}
if(msum > N) {
cout << -1;
return 0;
}
ll s = 0, e = msum, p = -1;
while(s <= e) {
ll m = (s + e)>>1;
if(check(m)) {
p = m;
e = m - 1;
}
else s = m + 1;
}
cout << p;
}
Compilation message
toilets.cpp: In function 'void rem(char, char, ll)':
toilets.cpp:24:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < tt[i].size(); j++) if(tt[i][j] == c) cnt++;
~~^~~~~~~~~~~~~~
toilets.cpp: In function 'void rev()':
toilets.cpp:74:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < T.size(); i++) {
~~^~~~~~~~~~
toilets.cpp: In function 'void print()':
toilets.cpp:82:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < T.size(); i++) {
~~^~~~~~~~~~
toilets.cpp: In function 'bool check(ll)':
toilets.cpp:117:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < T.size(); i++) {
~~^~~~~~~~~~
toilets.cpp:121:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < T[i].size(); j++) {
~~^~~~~~~~~~~~~
toilets.cpp: In function 'int main()':
toilets.cpp:152:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < S[i].size(); j++) {
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
5 ms |
3556 KB |
Output is correct |
3 |
Correct |
5 ms |
3556 KB |
Output is correct |
4 |
Correct |
5 ms |
3700 KB |
Output is correct |
5 |
Correct |
4 ms |
3776 KB |
Output is correct |
6 |
Correct |
5 ms |
3776 KB |
Output is correct |
7 |
Correct |
4 ms |
3776 KB |
Output is correct |
8 |
Correct |
5 ms |
3776 KB |
Output is correct |
9 |
Correct |
5 ms |
3836 KB |
Output is correct |
10 |
Correct |
4 ms |
3836 KB |
Output is correct |
11 |
Correct |
5 ms |
3836 KB |
Output is correct |
12 |
Correct |
6 ms |
3836 KB |
Output is correct |
13 |
Correct |
6 ms |
3836 KB |
Output is correct |
14 |
Correct |
6 ms |
3836 KB |
Output is correct |
15 |
Correct |
5 ms |
3836 KB |
Output is correct |
16 |
Correct |
5 ms |
3836 KB |
Output is correct |
17 |
Correct |
6 ms |
3836 KB |
Output is correct |
18 |
Correct |
5 ms |
3836 KB |
Output is correct |
19 |
Correct |
4 ms |
3836 KB |
Output is correct |
20 |
Correct |
4 ms |
3836 KB |
Output is correct |
21 |
Correct |
5 ms |
3836 KB |
Output is correct |
22 |
Correct |
5 ms |
3836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
5 ms |
3556 KB |
Output is correct |
3 |
Correct |
5 ms |
3556 KB |
Output is correct |
4 |
Correct |
5 ms |
3700 KB |
Output is correct |
5 |
Correct |
4 ms |
3776 KB |
Output is correct |
6 |
Correct |
5 ms |
3776 KB |
Output is correct |
7 |
Correct |
4 ms |
3776 KB |
Output is correct |
8 |
Correct |
5 ms |
3776 KB |
Output is correct |
9 |
Correct |
5 ms |
3836 KB |
Output is correct |
10 |
Correct |
4 ms |
3836 KB |
Output is correct |
11 |
Correct |
5 ms |
3836 KB |
Output is correct |
12 |
Correct |
6 ms |
3836 KB |
Output is correct |
13 |
Correct |
6 ms |
3836 KB |
Output is correct |
14 |
Correct |
6 ms |
3836 KB |
Output is correct |
15 |
Correct |
5 ms |
3836 KB |
Output is correct |
16 |
Correct |
5 ms |
3836 KB |
Output is correct |
17 |
Correct |
6 ms |
3836 KB |
Output is correct |
18 |
Correct |
5 ms |
3836 KB |
Output is correct |
19 |
Correct |
4 ms |
3836 KB |
Output is correct |
20 |
Correct |
4 ms |
3836 KB |
Output is correct |
21 |
Correct |
5 ms |
3836 KB |
Output is correct |
22 |
Correct |
5 ms |
3836 KB |
Output is correct |
23 |
Correct |
88 ms |
4660 KB |
Output is correct |
24 |
Correct |
112 ms |
4672 KB |
Output is correct |
25 |
Correct |
106 ms |
4680 KB |
Output is correct |
26 |
Correct |
36 ms |
4768 KB |
Output is correct |
27 |
Correct |
5 ms |
4768 KB |
Output is correct |
28 |
Correct |
131 ms |
4768 KB |
Output is correct |
29 |
Correct |
92 ms |
4768 KB |
Output is correct |
30 |
Correct |
104 ms |
4768 KB |
Output is correct |
31 |
Correct |
129 ms |
4800 KB |
Output is correct |
32 |
Correct |
126 ms |
4800 KB |
Output is correct |
33 |
Correct |
5 ms |
4800 KB |
Output is correct |
34 |
Correct |
45 ms |
4800 KB |
Output is correct |
35 |
Correct |
135 ms |
4800 KB |
Output is correct |
36 |
Correct |
154 ms |
4800 KB |
Output is correct |
37 |
Correct |
35 ms |
4800 KB |
Output is correct |
38 |
Correct |
6 ms |
4800 KB |
Output is correct |
39 |
Correct |
77 ms |
4800 KB |
Output is correct |
40 |
Correct |
73 ms |
4800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
5 ms |
3556 KB |
Output is correct |
3 |
Correct |
5 ms |
3556 KB |
Output is correct |
4 |
Correct |
5 ms |
3700 KB |
Output is correct |
5 |
Correct |
4 ms |
3776 KB |
Output is correct |
6 |
Correct |
5 ms |
3776 KB |
Output is correct |
7 |
Correct |
4 ms |
3776 KB |
Output is correct |
8 |
Correct |
5 ms |
3776 KB |
Output is correct |
9 |
Correct |
5 ms |
3836 KB |
Output is correct |
10 |
Correct |
4 ms |
3836 KB |
Output is correct |
11 |
Correct |
5 ms |
3836 KB |
Output is correct |
12 |
Correct |
6 ms |
3836 KB |
Output is correct |
13 |
Correct |
6 ms |
3836 KB |
Output is correct |
14 |
Correct |
6 ms |
3836 KB |
Output is correct |
15 |
Correct |
5 ms |
3836 KB |
Output is correct |
16 |
Correct |
5 ms |
3836 KB |
Output is correct |
17 |
Correct |
6 ms |
3836 KB |
Output is correct |
18 |
Correct |
5 ms |
3836 KB |
Output is correct |
19 |
Correct |
4 ms |
3836 KB |
Output is correct |
20 |
Correct |
4 ms |
3836 KB |
Output is correct |
21 |
Correct |
5 ms |
3836 KB |
Output is correct |
22 |
Correct |
5 ms |
3836 KB |
Output is correct |
23 |
Correct |
88 ms |
4660 KB |
Output is correct |
24 |
Correct |
112 ms |
4672 KB |
Output is correct |
25 |
Correct |
106 ms |
4680 KB |
Output is correct |
26 |
Correct |
36 ms |
4768 KB |
Output is correct |
27 |
Correct |
5 ms |
4768 KB |
Output is correct |
28 |
Correct |
131 ms |
4768 KB |
Output is correct |
29 |
Correct |
92 ms |
4768 KB |
Output is correct |
30 |
Correct |
104 ms |
4768 KB |
Output is correct |
31 |
Correct |
129 ms |
4800 KB |
Output is correct |
32 |
Correct |
126 ms |
4800 KB |
Output is correct |
33 |
Correct |
5 ms |
4800 KB |
Output is correct |
34 |
Correct |
45 ms |
4800 KB |
Output is correct |
35 |
Correct |
135 ms |
4800 KB |
Output is correct |
36 |
Correct |
154 ms |
4800 KB |
Output is correct |
37 |
Correct |
35 ms |
4800 KB |
Output is correct |
38 |
Correct |
6 ms |
4800 KB |
Output is correct |
39 |
Correct |
77 ms |
4800 KB |
Output is correct |
40 |
Correct |
73 ms |
4800 KB |
Output is correct |
41 |
Correct |
56 ms |
4800 KB |
Output is correct |
42 |
Correct |
744 ms |
12604 KB |
Output is correct |
43 |
Correct |
7 ms |
12604 KB |
Output is correct |
44 |
Correct |
5 ms |
12604 KB |
Output is correct |
45 |
Correct |
6 ms |
12604 KB |
Output is correct |
46 |
Correct |
365 ms |
12604 KB |
Output is correct |
47 |
Correct |
163 ms |
12604 KB |
Output is correct |
48 |
Correct |
133 ms |
12604 KB |
Output is correct |
49 |
Correct |
115 ms |
12604 KB |
Output is correct |
50 |
Correct |
75 ms |
12604 KB |
Output is correct |
51 |
Correct |
458 ms |
12604 KB |
Output is correct |
52 |
Correct |
755 ms |
15512 KB |
Output is correct |
53 |
Correct |
526 ms |
16472 KB |
Output is correct |
54 |
Correct |
598 ms |
17552 KB |
Output is correct |
55 |
Correct |
58 ms |
17552 KB |
Output is correct |
56 |
Correct |
68 ms |
17552 KB |
Output is correct |