# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
260070 | arnold518 | Vim (BOI13_vim) | C++14 | 1090 ms | 197880 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5000;
int N;
char A[MAXN+10];
ll dp[MAXN+10][MAXN+10];
vector<int> V[10];
vector<int>::iterator low[MAXN+10][10], upp[MAXN+10][10];
ll solve(int now, int pos)
{
ll &ret=dp[now][pos];
if(ret!=-1) return ret;
ret=1e18;
for(int i=0; i<10; i++)
{
if(i==4) continue;
auto it=upp[now][i];
if(it==V[i].end()) continue;
ret=min(ret, solve(*it, pos)+2);
}
if(now>pos)
{
ll p=now-pos+low[now][4]-low[pos][4];
auto jt=upp[now][4];
if(jt==V[4].end()) ret=min(ret, p);
else
{
for(int i=0; i<10; i++)
{
if(i==4) continue;
auto it=upp[pos+1][i];
if(it==V[i].end()) continue;
ret=min(ret, solve(*it, *jt)+p+2);
}
}
}
//printf("%d %d : %d\n", now, pos, ret);
return ret;
}
int main()
{
scanf("%*d", &N);
scanf(" %s", A+1);
N=strlen(A+1);
for(int i=1; i<=N; i++) V[A[i]-'a'].push_back(i);
for(int j=0; j<10; j++) for(int i=1; i<=N; i++) low[i][j]=lower_bound(V[j].begin(), V[j].end(), i);
for(int j=0; j<10; j++) for(int i=1; i<=N; i++) upp[i][j]=upper_bound(V[j].begin(), V[j].end(), i);
if(V[4].empty()) return !printf("0\n");
memset(dp, -1, sizeof(dp));
printf("%d\n", solve(1, V[4].front()));
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |