Submission #58152

# Submission time Handle Problem Language Result Execution time Memory
58152 2018-07-17T04:29:19 Z choikiwon Toilets (JOI16_toilets) C++17
100 / 100
634 ms 12644 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());
}

bool check(ll x) {
    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);
    rev();
    rem('F', 'M', 2 * N - 2 * msum);
    rev();

    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 'bool check(ll)':
toilets.cpp:100:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < T.size(); i++) {
                    ~~^~~~~~~~~~
toilets.cpp:104: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:135: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 3448 KB Output is correct
3 Correct 6 ms 3616 KB Output is correct
4 Correct 5 ms 3616 KB Output is correct
5 Correct 5 ms 3720 KB Output is correct
6 Correct 6 ms 3720 KB Output is correct
7 Correct 5 ms 3720 KB Output is correct
8 Correct 5 ms 3720 KB Output is correct
9 Correct 6 ms 3732 KB Output is correct
10 Correct 5 ms 3732 KB Output is correct
11 Correct 5 ms 3732 KB Output is correct
12 Correct 4 ms 3732 KB Output is correct
13 Correct 5 ms 3732 KB Output is correct
14 Correct 4 ms 3732 KB Output is correct
15 Correct 5 ms 3732 KB Output is correct
16 Correct 4 ms 3732 KB Output is correct
17 Correct 4 ms 3732 KB Output is correct
18 Correct 5 ms 3732 KB Output is correct
19 Correct 5 ms 3732 KB Output is correct
20 Correct 5 ms 3732 KB Output is correct
21 Correct 6 ms 3732 KB Output is correct
22 Correct 5 ms 3732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3448 KB Output is correct
2 Correct 5 ms 3448 KB Output is correct
3 Correct 6 ms 3616 KB Output is correct
4 Correct 5 ms 3616 KB Output is correct
5 Correct 5 ms 3720 KB Output is correct
6 Correct 6 ms 3720 KB Output is correct
7 Correct 5 ms 3720 KB Output is correct
8 Correct 5 ms 3720 KB Output is correct
9 Correct 6 ms 3732 KB Output is correct
10 Correct 5 ms 3732 KB Output is correct
11 Correct 5 ms 3732 KB Output is correct
12 Correct 4 ms 3732 KB Output is correct
13 Correct 5 ms 3732 KB Output is correct
14 Correct 4 ms 3732 KB Output is correct
15 Correct 5 ms 3732 KB Output is correct
16 Correct 4 ms 3732 KB Output is correct
17 Correct 4 ms 3732 KB Output is correct
18 Correct 5 ms 3732 KB Output is correct
19 Correct 5 ms 3732 KB Output is correct
20 Correct 5 ms 3732 KB Output is correct
21 Correct 6 ms 3732 KB Output is correct
22 Correct 5 ms 3732 KB Output is correct
23 Correct 80 ms 4696 KB Output is correct
24 Correct 101 ms 4696 KB Output is correct
25 Correct 106 ms 4900 KB Output is correct
26 Correct 39 ms 4900 KB Output is correct
27 Correct 6 ms 4900 KB Output is correct
28 Correct 102 ms 4900 KB Output is correct
29 Correct 108 ms 4900 KB Output is correct
30 Correct 89 ms 4900 KB Output is correct
31 Correct 109 ms 4900 KB Output is correct
32 Correct 106 ms 4900 KB Output is correct
33 Correct 6 ms 4900 KB Output is correct
34 Correct 48 ms 4900 KB Output is correct
35 Correct 113 ms 4900 KB Output is correct
36 Correct 116 ms 4900 KB Output is correct
37 Correct 35 ms 4900 KB Output is correct
38 Correct 6 ms 4900 KB Output is correct
39 Correct 83 ms 4900 KB Output is correct
40 Correct 86 ms 4900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3448 KB Output is correct
2 Correct 5 ms 3448 KB Output is correct
3 Correct 6 ms 3616 KB Output is correct
4 Correct 5 ms 3616 KB Output is correct
5 Correct 5 ms 3720 KB Output is correct
6 Correct 6 ms 3720 KB Output is correct
7 Correct 5 ms 3720 KB Output is correct
8 Correct 5 ms 3720 KB Output is correct
9 Correct 6 ms 3732 KB Output is correct
10 Correct 5 ms 3732 KB Output is correct
11 Correct 5 ms 3732 KB Output is correct
12 Correct 4 ms 3732 KB Output is correct
13 Correct 5 ms 3732 KB Output is correct
14 Correct 4 ms 3732 KB Output is correct
15 Correct 5 ms 3732 KB Output is correct
16 Correct 4 ms 3732 KB Output is correct
17 Correct 4 ms 3732 KB Output is correct
18 Correct 5 ms 3732 KB Output is correct
19 Correct 5 ms 3732 KB Output is correct
20 Correct 5 ms 3732 KB Output is correct
21 Correct 6 ms 3732 KB Output is correct
22 Correct 5 ms 3732 KB Output is correct
23 Correct 80 ms 4696 KB Output is correct
24 Correct 101 ms 4696 KB Output is correct
25 Correct 106 ms 4900 KB Output is correct
26 Correct 39 ms 4900 KB Output is correct
27 Correct 6 ms 4900 KB Output is correct
28 Correct 102 ms 4900 KB Output is correct
29 Correct 108 ms 4900 KB Output is correct
30 Correct 89 ms 4900 KB Output is correct
31 Correct 109 ms 4900 KB Output is correct
32 Correct 106 ms 4900 KB Output is correct
33 Correct 6 ms 4900 KB Output is correct
34 Correct 48 ms 4900 KB Output is correct
35 Correct 113 ms 4900 KB Output is correct
36 Correct 116 ms 4900 KB Output is correct
37 Correct 35 ms 4900 KB Output is correct
38 Correct 6 ms 4900 KB Output is correct
39 Correct 83 ms 4900 KB Output is correct
40 Correct 86 ms 4900 KB Output is correct
41 Correct 37 ms 4900 KB Output is correct
42 Correct 578 ms 12608 KB Output is correct
43 Correct 5 ms 12608 KB Output is correct
44 Correct 6 ms 12608 KB Output is correct
45 Correct 6 ms 12608 KB Output is correct
46 Correct 291 ms 12608 KB Output is correct
47 Correct 121 ms 12608 KB Output is correct
48 Correct 119 ms 12608 KB Output is correct
49 Correct 88 ms 12608 KB Output is correct
50 Correct 79 ms 12608 KB Output is correct
51 Correct 413 ms 12608 KB Output is correct
52 Correct 634 ms 12608 KB Output is correct
53 Correct 560 ms 12608 KB Output is correct
54 Correct 554 ms 12644 KB Output is correct
55 Correct 83 ms 12644 KB Output is correct
56 Correct 63 ms 12644 KB Output is correct