This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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;
~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |