Submission #114347

#TimeUsernameProblemLanguageResultExecution timeMemory
114347onjo0127Toilets (JOI16_toilets)C++11
100 / 100
131 ms7480 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...