Submission #58144

# Submission time Handle Problem Language Result Execution time Memory
58144 2018-07-17T04:00:40 Z choikiwon Toilets (JOI16_toilets) C++17
36 / 100
552 ms 19760 KB
#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 = 0;
        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:73: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:81: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:116:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < T.size(); i++) {
                    ~~^~~~~~~~~~
toilets.cpp:120: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 3532 KB Output is correct
2 Correct 4 ms 3684 KB Output is correct
3 Correct 5 ms 3796 KB Output is correct
4 Correct 6 ms 3796 KB Output is correct
5 Correct 5 ms 3948 KB Output is correct
6 Correct 4 ms 3948 KB Output is correct
7 Correct 5 ms 3952 KB Output is correct
8 Correct 5 ms 3952 KB Output is correct
9 Correct 5 ms 3952 KB Output is correct
10 Correct 6 ms 3952 KB Output is correct
11 Correct 6 ms 3952 KB Output is correct
12 Correct 6 ms 3952 KB Output is correct
13 Correct 6 ms 3952 KB Output is correct
14 Correct 6 ms 3952 KB Output is correct
15 Correct 6 ms 3952 KB Output is correct
16 Correct 5 ms 3952 KB Output is correct
17 Correct 7 ms 3952 KB Output is correct
18 Correct 6 ms 3952 KB Output is correct
19 Correct 6 ms 3952 KB Output is correct
20 Correct 6 ms 3952 KB Output is correct
21 Correct 6 ms 3952 KB Output is correct
22 Correct 5 ms 4000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3532 KB Output is correct
2 Correct 4 ms 3684 KB Output is correct
3 Correct 5 ms 3796 KB Output is correct
4 Correct 6 ms 3796 KB Output is correct
5 Correct 5 ms 3948 KB Output is correct
6 Correct 4 ms 3948 KB Output is correct
7 Correct 5 ms 3952 KB Output is correct
8 Correct 5 ms 3952 KB Output is correct
9 Correct 5 ms 3952 KB Output is correct
10 Correct 6 ms 3952 KB Output is correct
11 Correct 6 ms 3952 KB Output is correct
12 Correct 6 ms 3952 KB Output is correct
13 Correct 6 ms 3952 KB Output is correct
14 Correct 6 ms 3952 KB Output is correct
15 Correct 6 ms 3952 KB Output is correct
16 Correct 5 ms 3952 KB Output is correct
17 Correct 7 ms 3952 KB Output is correct
18 Correct 6 ms 3952 KB Output is correct
19 Correct 6 ms 3952 KB Output is correct
20 Correct 6 ms 3952 KB Output is correct
21 Correct 6 ms 3952 KB Output is correct
22 Correct 5 ms 4000 KB Output is correct
23 Correct 105 ms 5148 KB Output is correct
24 Correct 123 ms 5376 KB Output is correct
25 Correct 94 ms 5604 KB Output is correct
26 Correct 40 ms 5808 KB Output is correct
27 Correct 7 ms 5808 KB Output is correct
28 Correct 96 ms 6232 KB Output is correct
29 Correct 94 ms 6420 KB Output is correct
30 Correct 95 ms 6648 KB Output is correct
31 Correct 122 ms 6876 KB Output is correct
32 Correct 111 ms 7084 KB Output is correct
33 Correct 7 ms 7084 KB Output is correct
34 Correct 59 ms 7396 KB Output is correct
35 Correct 116 ms 7596 KB Output is correct
36 Correct 129 ms 7868 KB Output is correct
37 Correct 38 ms 7980 KB Output is correct
38 Correct 8 ms 7980 KB Output is correct
39 Correct 88 ms 8456 KB Output is correct
40 Correct 80 ms 8568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3532 KB Output is correct
2 Correct 4 ms 3684 KB Output is correct
3 Correct 5 ms 3796 KB Output is correct
4 Correct 6 ms 3796 KB Output is correct
5 Correct 5 ms 3948 KB Output is correct
6 Correct 4 ms 3948 KB Output is correct
7 Correct 5 ms 3952 KB Output is correct
8 Correct 5 ms 3952 KB Output is correct
9 Correct 5 ms 3952 KB Output is correct
10 Correct 6 ms 3952 KB Output is correct
11 Correct 6 ms 3952 KB Output is correct
12 Correct 6 ms 3952 KB Output is correct
13 Correct 6 ms 3952 KB Output is correct
14 Correct 6 ms 3952 KB Output is correct
15 Correct 6 ms 3952 KB Output is correct
16 Correct 5 ms 3952 KB Output is correct
17 Correct 7 ms 3952 KB Output is correct
18 Correct 6 ms 3952 KB Output is correct
19 Correct 6 ms 3952 KB Output is correct
20 Correct 6 ms 3952 KB Output is correct
21 Correct 6 ms 3952 KB Output is correct
22 Correct 5 ms 4000 KB Output is correct
23 Correct 105 ms 5148 KB Output is correct
24 Correct 123 ms 5376 KB Output is correct
25 Correct 94 ms 5604 KB Output is correct
26 Correct 40 ms 5808 KB Output is correct
27 Correct 7 ms 5808 KB Output is correct
28 Correct 96 ms 6232 KB Output is correct
29 Correct 94 ms 6420 KB Output is correct
30 Correct 95 ms 6648 KB Output is correct
31 Correct 122 ms 6876 KB Output is correct
32 Correct 111 ms 7084 KB Output is correct
33 Correct 7 ms 7084 KB Output is correct
34 Correct 59 ms 7396 KB Output is correct
35 Correct 116 ms 7596 KB Output is correct
36 Correct 129 ms 7868 KB Output is correct
37 Correct 38 ms 7980 KB Output is correct
38 Correct 8 ms 7980 KB Output is correct
39 Correct 88 ms 8456 KB Output is correct
40 Correct 80 ms 8568 KB Output is correct
41 Correct 45 ms 10028 KB Output is correct
42 Incorrect 552 ms 19760 KB Output isn't correct
43 Halted 0 ms 0 KB -