Submission #329558

# Submission time Handle Problem Language Result Execution time Memory
329558 2020-11-21T15:11:19 Z gs18115 Vim (BOI13_vim) C++14
100 / 100
58 ms 24812 KB
#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
const int mx=70010;
inline void app(int&x,int y)
{
    x=min(x,y);
    return;
}
int s[mx];
bool chk[mx];
int nxt[mx][10];
int dp1[mx][10];
int dp2[mx][10][10];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    int ce=0;
    for(int i=0;i<n;i++)
    {
        char c;
        cin>>c;
        if(c=='e')
            chk[i-ce]=1,ce++;
        else
            s[i-ce]=c<'e'?c-'a':c-'a'-1;
    }
    if(ce==0)
        return cout<<0<<endl,0;
    n-=ce;
    for(int i=0;i<10;i++)
        nxt[n-1][i]=-1;
    for(int i=n-1;i-->0;)
        for(int j=0;j<10;j++)
            nxt[i][j]=s[i+1]==j?i+1:nxt[i+1][j];
    for(int i=0;i<10;i++)
        dp1[0][i]=2;
    for(int i=0;i<10;i++)
        for(int j=0;j<10;j++)
            dp2[0][i][j]=nxt[0][i]==-1?inf:2+2+nxt[0][i];
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<10;j++)
            dp1[i][j]=inf;
        for(int j=0;j<10;j++)
            for(int k=0;k<10;k++)
                dp2[i][j][k]=inf;
        { // 1->1
            if(!chk[i])
                for(int j=0;j<10;j++)
                    if(s[i]!=j)
                        app(dp1[i][j],dp1[i-1][j]);
            for(int j=0;j<10;j++)
                app(dp1[i][j],dp1[i-1][s[i]]+2);
        }
        { // 1->2
            for(int j=0;j<10;j++)
                if(nxt[i][j]!=1&&s[i]!=j)
                    for(int k=0;k<10;k++)
                            app(dp2[i][j][k],dp1[i-1][j]+2+nxt[i][j]-i);
            for(int j=0;j<10;j++)
                if(nxt[i][j]!=-1)
                    for(int k=0;k<10;k++)
                        app(dp2[i][j][k],dp1[i-1][s[i]]+2+2+nxt[i][j]-i);
        }
        { // 2->1
            for(int j=0;j<10;j++)
                if(s[i]!=j)
                    app(dp1[i][j],dp2[i-1][s[i]][j]);
            for(int j=0;j<10;j++)
                app(dp1[i][j],dp2[i-1][s[i]][s[i]]+2);
        }
        { // 2->2
            for(int j=0;j<10;j++)
                if(nxt[i][j]!=-1&&s[i]!=j)
                    for(int k=0;k<10;k++)
                        if(s[i]!=k)
                            app(dp2[i][j][k],dp2[i-1][j][k]);
            for(int j=0;j<10;j++)
                if(nxt[i][j]!=-1)
                    for(int k=0;k<10;k++)
                        if(s[i]!=k)
                            app(dp2[i][j][k],dp2[i-1][s[i]][k]+2+nxt[i][j]-i);
            for(int j=0;j<10;j++)
                if(nxt[i][j]!=-1&&s[i]!=j)
                    for(int k=0;k<10;k++)
                        app(dp2[i][j][k],dp2[i-1][j][s[i]]+2);
            for(int j=0;j<10;j++)
                if(nxt[i][j]!=-1)
                    for(int k=0;k<10;k++)
                        app(dp2[i][j][k],dp2[i-1][s[i]][s[i]]+2+2+nxt[i][j]-i);
        }
    }
    cout<<dp1[n-1][9]+2*ce-2<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 620 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1260 KB Output is correct
2 Correct 3 ms 1644 KB Output is correct
3 Correct 2 ms 1004 KB Output is correct
4 Correct 3 ms 1260 KB Output is correct
5 Correct 4 ms 1644 KB Output is correct
6 Correct 5 ms 2156 KB Output is correct
7 Correct 3 ms 1516 KB Output is correct
8 Correct 3 ms 1516 KB Output is correct
9 Correct 3 ms 1644 KB Output is correct
10 Correct 3 ms 1644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 21612 KB Output is correct
2 Correct 42 ms 20716 KB Output is correct
3 Correct 47 ms 21868 KB Output is correct
4 Correct 45 ms 22252 KB Output is correct
5 Correct 56 ms 24684 KB Output is correct
6 Correct 28 ms 11372 KB Output is correct
7 Correct 42 ms 18540 KB Output is correct
8 Correct 50 ms 21612 KB Output is correct
9 Correct 36 ms 15980 KB Output is correct
10 Correct 39 ms 15340 KB Output is correct
11 Correct 43 ms 22252 KB Output is correct
12 Correct 58 ms 24812 KB Output is correct
13 Correct 52 ms 24300 KB Output is correct
14 Correct 49 ms 23916 KB Output is correct
15 Correct 47 ms 20076 KB Output is correct
16 Correct 44 ms 21868 KB Output is correct
17 Correct 49 ms 21612 KB Output is correct
18 Correct 48 ms 21868 KB Output is correct
19 Correct 42 ms 20716 KB Output is correct
20 Correct 40 ms 17516 KB Output is correct