Submission #243409

# Submission time Handle Problem Language Result Execution time Memory
243409 2020-07-01T03:36:38 Z ffao Vim (BOI13_vim) C++14
56.4444 / 100
777 ms 71872 KB
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n;
string s;

#define JMPS 2

int ccE;
int nxtnE[71000];
int nxt[71000][21];
int idxE[71000];
int jmp[71000][11][JMPS+1];
int dist[71000][11][JMPS+1];
int spt[71000][10][17];

priority_queue< pair<int, pair<int, pii> > > q;

void update(int nextE, int let, int idx, int curDist) {
    if (dist[nextE][let][idx] > curDist) {
        dist[nextE][let][idx] = curDist;
        q.push( {-curDist, {nextE, {let, idx}}} );
    }
}

int adv(int curPos, int lastE) {
    int addD = 0;
    int ret = 1000000000;

    for (int j = 0; j < 9; j++) {
        for (int t = 16; t >= 0; t--) {
            if (spt[curPos][j][t] <= lastE) {
                addD += (1<<t) * 2;
                curPos = spt[curPos][j][t];
            }
        }

        for (int jj = 0; jj <= j; jj++) if (spt[curPos][jj][0] != n) {
            ret = min(ret, addD + 2 + (spt[curPos][jj][0] - lastE));
        }
    }

    return ret;
}

void expand(int curPos, int nextE, int curDist) {
    // cout << "EXP" << endl;
    
    int addD = 0;
    for (int j = 0; j < 9; j++) {
        for (int t = 16; t >= 0; t--) {
            if (spt[curPos][j][t] <= nextE) {
                addD += (1<<t) * 2;
                curPos = spt[curPos][j][t];
            }
        }

        for (int jj = 0; jj <= j; jj++) if (spt[curPos][jj][0] != n) {
            int letdst = s[ spt[curPos][jj][0] ] - 'a';
            update(nextE, letdst, 0, curDist + addD + 2);
        }
    }
    
    // cout << "EXPD" << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> s;

    int st = 0;
    while (st < n && s[st] == 'e') st++;

    int rem = st;
    s = s.substr(st);
    n = (int)s.size();

    memset(dist, 0x3f, sizeof(dist));

    for (int j = 0; j < 10; j++) nxt[n][j] = n;
    for (int i = n-1; i >= 0; i--) {
        for (int j = 0; j < 10; j++) nxt[i][j] = nxt[i+1][j];
        nxt[i][s[i]-'a'] = i;
    }

    for (int i = 0; i < n; i++) if (s[i] == 'e') {
        nxtnE[i] = n;
        for (int j = 0; j < 10; j++) if (j != 4) {
            nxtnE[i] = min(nxtnE[i], nxt[i][j]);
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 10; j++) {
            jmp[i][j][0] = i;
            for (int k = 1; k <= JMPS; k++) {
                jmp[i][j][k] = jmp[i][j][k-1];
                if (jmp[i][j][k] < n) jmp[i][j][k] = nxt[jmp[i][j][k]+1][j];
            }
        }
    }

    for (int i = 0; i < n; i++) {
        vector<int> dests;
        for (int j = 0; j < 10; j++) if (j != 4) {
            dests.push_back(nxt[i+1][j]);
        }
        sort(all(dests));
        for (int j = 0; j < 9; j++) {
            spt[i][j][0] = dests[8-j];
        }
    }

    for (int j = 0; j < 9; j++) spt[n][j][0] = n;

    for (int k = 1; k < 17; k++) {
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j < 9; j++) {
                spt[i][j][k] = spt[ spt[i][j][k-1] ][j][k-1];
            }    
        }
    }

    int stE = nxt[0][4];
    if (stE == n) {
        cout << rem << endl;
        return 0;
    }

    int pE = stE;
    idxE[pE] = ccE++;
    while (pE < n) {
        pE = nxt[pE+1][4];
        idxE[pE] = ccE++;
    }

    int lastE = n;
    for (int i = n-1; i >= 0; i--) if (s[i]=='e') { lastE = i; break; }

    expand(0, stE, 0);
    
    int ans = 1000000000;

    // adv(3, lastE);
    // return 0;

    while (!q.empty()) {
        pair<int, pair<int, pii> > entry = q.top(); q.pop();
        int nextE = entry.second.first;
        int let = entry.second.second.first;
        int letidx = entry.second.second.second;
        int curDist = -entry.first;

        // cout << nextE << " " << let << " " << letidx << " " << curDist << endl;
        if (dist[nextE][let][letidx] != curDist) continue;

        int thisPos = jmp[nextE][let][letidx+1];

        // go back and fill up to nextE
        int nwNextE = nxt[thisPos+1][4];
        int cntEs = idxE[nwNextE] - idxE[nextE];

        if (nwNextE == n) {
            ans = min(ans, curDist + cntEs + (thisPos-nextE));
        }
        else {
            //cout << "thisPos " << thisPos << " adv " << adv(thisPos, lastE) << " Ed " << lastE - nextE << endl;
            ans = min(ans, curDist + adv(thisPos, lastE) + (idxE[n] - idxE[nextE]) + (lastE - nextE));
            expand(nxtnE[nextE], nwNextE, curDist + cntEs + (thisPos-nextE));
        }

        // go forward
        for (int j = 0; j < 10; j++) if (j != 4) {
            int np = nxt[thisPos+1][j];
            if (np == n) continue;
            int nIdx = -1;
            for (int k = 1; k <= JMPS; k++) {
                if (jmp[nextE][j][k] == np) nIdx = k-1;
            }

            if (nIdx != -1) update(nextE, j, nIdx, curDist + 2);
        }
    }

    cout << ans + rem << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9984 KB Output is correct
2 Correct 15 ms 9984 KB Output is correct
3 Incorrect 11 ms 9984 KB Output isn't correct
4 Correct 13 ms 9984 KB Output is correct
5 Correct 13 ms 9984 KB Output is correct
6 Correct 15 ms 9984 KB Output is correct
7 Correct 13 ms 9984 KB Output is correct
8 Correct 9 ms 9600 KB Output is correct
9 Correct 10 ms 9600 KB Output is correct
10 Correct 9 ms 9600 KB Output is correct
11 Correct 9 ms 9600 KB Output is correct
12 Correct 9 ms 9600 KB Output is correct
13 Correct 13 ms 9984 KB Output is correct
14 Correct 12 ms 9960 KB Output is correct
15 Incorrect 11 ms 10112 KB Output isn't correct
16 Correct 11 ms 9984 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 12 ms 9856 KB Output is correct
19 Correct 12 ms 9856 KB Output is correct
20 Correct 12 ms 9856 KB Output is correct
21 Correct 13 ms 9856 KB Output is correct
22 Correct 13 ms 9984 KB Output is correct
23 Correct 11 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 12 ms 9984 KB Output is correct
27 Correct 14 ms 9984 KB Output is correct
28 Correct 15 ms 9984 KB Output is correct
29 Correct 13 ms 9984 KB Output is correct
30 Incorrect 12 ms 9984 KB Output isn't correct
31 Correct 12 ms 9984 KB Output is correct
32 Incorrect 12 ms 9984 KB Output isn't correct
33 Correct 13 ms 9984 KB Output is correct
34 Correct 14 ms 9984 KB Output is correct
35 Correct 14 ms 9984 KB Output is correct
36 Correct 13 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 13952 KB Output is correct
2 Correct 64 ms 14060 KB Output is correct
3 Correct 30 ms 12152 KB Output is correct
4 Correct 44 ms 13952 KB Output is correct
5 Correct 57 ms 13952 KB Output is correct
6 Correct 40 ms 13952 KB Output is correct
7 Correct 45 ms 13952 KB Output is correct
8 Incorrect 46 ms 13952 KB Output isn't correct
9 Correct 64 ms 14072 KB Output is correct
10 Incorrect 53 ms 14092 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 478 ms 63264 KB Output isn't correct
2 Incorrect 412 ms 62200 KB Output isn't correct
3 Incorrect 480 ms 63480 KB Output isn't correct
4 Incorrect 537 ms 71796 KB Output isn't correct
5 Correct 556 ms 71540 KB Output is correct
6 Incorrect 510 ms 64248 KB Output isn't correct
7 Incorrect 708 ms 66292 KB Output isn't correct
8 Incorrect 591 ms 67448 KB Output isn't correct
9 Incorrect 584 ms 67192 KB Output isn't correct
10 Incorrect 594 ms 67704 KB Output isn't correct
11 Incorrect 501 ms 71872 KB Output isn't correct
12 Correct 554 ms 71488 KB Output is correct
13 Incorrect 549 ms 69748 KB Output isn't correct
14 Incorrect 480 ms 70388 KB Output isn't correct
15 Incorrect 777 ms 71156 KB Output isn't correct
16 Incorrect 344 ms 62968 KB Output isn't correct
17 Incorrect 483 ms 63352 KB Output isn't correct
18 Incorrect 452 ms 63352 KB Output isn't correct
19 Incorrect 421 ms 62072 KB Output isn't correct
20 Incorrect 675 ms 62840 KB Output isn't correct