답안 #243096

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
243096 2020-06-30T10:22:22 Z ffao Vim (BOI13_vim) C++14
76 / 100
2000 ms 134516 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 12

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}}} );
    }
}

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 letdst = 0; letdst < 10; letdst++) if (letdst != 4 && nxt[nextE][letdst] != n && nxt[curPos+1][letdst] == nxt[nextE][letdst]) {
            // if (curPos == 0) cout << curPos << " " << nextE << " " << curDist << " -> " << letdst << " " << nxt[curPos+1][letdst] << " " << curDist + addD << endl;
            update(nextE, letdst, 0, curDist + addD + 2);
        }
    }

    // for (int letdst = 0; letdst < 10; letdst++) if (letdst != 4 && nxt[nextE][letdst] != n) {
    //     int dest = nxt[nextE][letdst];
    //     int pos = curPos, tDist = curDist;
    //     while (pos < dest) {
    //         int nP = pos;
    //         for (int tt = 0; tt < 10; tt++) if (tt != 4) {
    //             if (nxt[pos+1][tt] <= dest) nP = max(nP, nxt[pos+1][tt]);
    //         }
    //         tDist+=2;
    //         pos = nP;
    //     }

    //     cout << curPos << " " << nextE << " " << curDist << " -> " << letdst << " " << dest << " " << tDist << endl;
    //     update(nextE, letdst, 0, tDist);
    // }
    
    // 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++;
    }

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

    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 {
            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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 40832 KB Output is correct
2 Correct 31 ms 40704 KB Output is correct
3 Correct 28 ms 40704 KB Output is correct
4 Correct 35 ms 40832 KB Output is correct
5 Correct 38 ms 40824 KB Output is correct
6 Correct 47 ms 41080 KB Output is correct
7 Correct 40 ms 40956 KB Output is correct
8 Correct 27 ms 40192 KB Output is correct
9 Correct 29 ms 40184 KB Output is correct
10 Correct 25 ms 40064 KB Output is correct
11 Correct 26 ms 40192 KB Output is correct
12 Correct 25 ms 40064 KB Output is correct
13 Correct 34 ms 40832 KB Output is correct
14 Correct 31 ms 40704 KB Output is correct
15 Correct 30 ms 40696 KB Output is correct
16 Correct 33 ms 40704 KB Output is correct
17 Correct 38 ms 40960 KB Output is correct
18 Correct 35 ms 40696 KB Output is correct
19 Correct 31 ms 40568 KB Output is correct
20 Correct 34 ms 40824 KB Output is correct
21 Correct 38 ms 40696 KB Output is correct
22 Correct 41 ms 40832 KB Output is correct
23 Correct 30 ms 40832 KB Output is correct
24 Correct 35 ms 40832 KB Output is correct
25 Correct 34 ms 40832 KB Output is correct
26 Correct 35 ms 40960 KB Output is correct
27 Correct 45 ms 41080 KB Output is correct
28 Correct 45 ms 40952 KB Output is correct
29 Correct 39 ms 40832 KB Output is correct
30 Correct 34 ms 40832 KB Output is correct
31 Correct 35 ms 40832 KB Output is correct
32 Correct 35 ms 40952 KB Output is correct
33 Correct 39 ms 41088 KB Output is correct
34 Correct 42 ms 40952 KB Output is correct
35 Correct 42 ms 40960 KB Output is correct
36 Correct 39 ms 40832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 47224 KB Output is correct
2 Correct 288 ms 47228 KB Output is correct
3 Correct 144 ms 44152 KB Output is correct
4 Correct 183 ms 46972 KB Output is correct
5 Correct 252 ms 47096 KB Output is correct
6 Correct 173 ms 47096 KB Output is correct
7 Correct 183 ms 46976 KB Output is correct
8 Correct 202 ms 46972 KB Output is correct
9 Correct 277 ms 47096 KB Output is correct
10 Correct 229 ms 47096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1864 ms 120152 KB Output is correct
2 Correct 1686 ms 118356 KB Output is correct
3 Correct 1955 ms 120312 KB Output is correct
4 Execution timed out 2050 ms 134516 KB Time limit exceeded
5 Execution timed out 2106 ms 132212 KB Time limit exceeded
6 Execution timed out 2085 ms 121508 KB Time limit exceeded
7 Execution timed out 2108 ms 124536 KB Time limit exceeded
8 Execution timed out 2104 ms 126452 KB Time limit exceeded
9 Execution timed out 2101 ms 127296 KB Time limit exceeded
10 Execution timed out 2107 ms 126584 KB Time limit exceeded
11 Execution timed out 2009 ms 134508 KB Time limit exceeded
12 Execution timed out 2101 ms 132336 KB Time limit exceeded
13 Execution timed out 2108 ms 129524 KB Time limit exceeded
14 Correct 1935 ms 130676 KB Output is correct
15 Execution timed out 2099 ms 131572 KB Time limit exceeded
16 Correct 1288 ms 120848 KB Output is correct
17 Correct 1837 ms 120056 KB Output is correct
18 Correct 1927 ms 120548 KB Output is correct
19 Correct 1662 ms 118392 KB Output is correct
20 Execution timed out 2096 ms 119288 KB Time limit exceeded