답안 #256493

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256493 2020-08-02T18:39:36 Z shayan_p Vim (BOI13_vim) C++14
100 / 100
631 ms 9336 KB
// And you curse yourself for things you never done

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

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

const int maxn = 70100, mod = 1e9 + 7, inf = 1e9 + 10;

int nxt[maxn][10], nxt2[maxn][10], lst[10], a[maxn];
int dp[maxn][10], DP[maxn], DP2[maxn], DP3[maxn];
vector<int> poses;

int dis(int a, int b){
    if(a > b)
	return inf;
    if(a == b)
	return 0;
    int mx = a;
    for(int i = 0; i < 9; i++){
	if(nxt[a][i] <= b)
	    mx = max(mx, nxt[a][i]);
    }
    return 2 + dis(mx, b);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();

    int n;
    cin >> n;
    string _s, s;
    cin >> _s;
    int ans = 0;
    for(int i = 0; i < n; i++){
	if(_s[i] == 'e')
	    ans+= 2;
	if(_s[i] == 'e' && _s[i + 1] != 'e')
	    poses.PB(sz(s));
	if(_s[i] != 'e')
	    s+= _s[i];	
    }
    memset(lst, -1, sizeof lst);
    memset(nxt, -1, sizeof nxt);
    n = sz(s);
    for(int i = n-1; i >= 0; i--){
	for(int j = 0; j < 9; j++)
	    nxt[i][j] = (lst[j] == -1 ? i : lst[j]);
	int id = (s[i] - 'a') - (s[i] > 'e');
	a[i] = id;
	lst[id] = i;
	for(int j = 0; j < 9; j++)
	    nxt2[i][j] = (lst[j] == -1 ? i : lst[j]);
    }

    for(int i = 0; i < maxn; i++){
	for(int j = 0; j < 10; j++)
	    dp[i][j] = inf;
	DP[i] = DP2[i] = DP3[i] = inf;
    }
    for(int i = poses.back(); i < maxn; i++){
	DP[i] = DP2[i] = DP3[i] = 0;
    }
    
    int pt = sz(poses) - 1;
    for(int i = poses.back() - 1; i >= 0; i--){
	for(int j = 0; j < 9; j++){
	    if(nxt[i][j] > i){
		DP3[i] = min(DP3[i], 2 + DP3[ nxt[i][j] ] + nxt[i][j] - i);		
	    }
	}
	for(int j = 0; j < 9; j++){
	    if(nxt[i][j] > i){
		if(nxt[i][j] < poses[pt]) // <= ? 
		    DP2[i] = min(DP2[i], 2 + DP2[ nxt[i][j] ]);
		else
		    DP2[i] = min(DP2[i], 2 + DP3[ nxt[i][j] ] + nxt[i][j] - poses[pt]);		
	    }
	}
	DP[i] = DP2[i];
	for(int j = 0; j < 9; j++){
	    int x = nxt[i][j];
	    if(x == i)
		continue;
	    if(x <= poses[pt]){
		DP[i] = min(DP[i], 2 + DP[x]);
	    }
	    else{
		for(int k = 0; k < 9; k++){
		    DP[i] = min(DP[i], 2 + x - poses[pt] + dis(poses[pt], nxt2[x][k]) + dp[x][k]);
		}
	    }
	}
	for(int j = 0; j < 9; j++){
	    int x = nxt2[i][j];
	    if(x <= poses[pt]){
		dp[i][j] = DP[x];
	    }
	    else{
		dp[i][j] = x - poses[pt] + DP3[x];
		for(int k = 0; k < 9; k++)
		    dp[i][j] = min(dp[i][j], x - poses[pt] + dis(poses[pt], nxt2[x][k]) + dp[x][k]);
	    }
	    for(int k = 0; k < 9; k++){
		int xx = nxt[i][k];
		if(xx == i)
		    continue;
		for(int k2 = 0; k2 < 9; k2++)
		    dp[i][j] = min(dp[i][j], 2 + xx - i + dis(x, nxt2[xx][k2]) + dp[xx][k2]);
	    }
	}
	if(pt > 0 && poses[pt-1] == i){
	    pt--;
	}
    }
    ans+= DP[0];
    return cout << ans << "\n", 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6656 KB Output is correct
2 Correct 5 ms 6656 KB Output is correct
3 Correct 5 ms 6656 KB Output is correct
4 Correct 6 ms 6656 KB Output is correct
5 Correct 7 ms 6656 KB Output is correct
6 Correct 8 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 3 ms 6656 KB Output is correct
9 Correct 4 ms 6656 KB Output is correct
10 Correct 4 ms 6656 KB Output is correct
11 Correct 4 ms 6656 KB Output is correct
12 Correct 4 ms 6656 KB Output is correct
13 Correct 8 ms 6656 KB Output is correct
14 Correct 7 ms 6656 KB Output is correct
15 Correct 5 ms 6656 KB Output is correct
16 Correct 5 ms 6656 KB Output is correct
17 Correct 8 ms 6656 KB Output is correct
18 Correct 7 ms 6656 KB Output is correct
19 Correct 6 ms 6656 KB Output is correct
20 Correct 6 ms 6656 KB Output is correct
21 Correct 10 ms 6656 KB Output is correct
22 Correct 7 ms 6656 KB Output is correct
23 Correct 6 ms 6656 KB Output is correct
24 Correct 7 ms 6656 KB Output is correct
25 Correct 8 ms 6656 KB Output is correct
26 Correct 7 ms 6656 KB Output is correct
27 Correct 8 ms 6656 KB Output is correct
28 Correct 7 ms 6656 KB Output is correct
29 Correct 7 ms 6656 KB Output is correct
30 Correct 10 ms 6656 KB Output is correct
31 Correct 8 ms 6656 KB Output is correct
32 Correct 8 ms 6656 KB Output is correct
33 Correct 7 ms 6656 KB Output is correct
34 Correct 8 ms 6656 KB Output is correct
35 Correct 7 ms 6656 KB Output is correct
36 Correct 7 ms 6656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 6784 KB Output is correct
2 Correct 45 ms 6864 KB Output is correct
3 Correct 22 ms 6784 KB Output is correct
4 Correct 30 ms 6784 KB Output is correct
5 Correct 41 ms 6784 KB Output is correct
6 Correct 44 ms 6784 KB Output is correct
7 Correct 32 ms 6904 KB Output is correct
8 Correct 35 ms 6784 KB Output is correct
9 Correct 43 ms 6784 KB Output is correct
10 Correct 38 ms 6784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 543 ms 8940 KB Output is correct
2 Correct 462 ms 8928 KB Output is correct
3 Correct 529 ms 9080 KB Output is correct
4 Correct 367 ms 9208 KB Output is correct
5 Correct 622 ms 9216 KB Output is correct
6 Correct 348 ms 8064 KB Output is correct
7 Correct 508 ms 8704 KB Output is correct
8 Correct 535 ms 9036 KB Output is correct
9 Correct 424 ms 8500 KB Output is correct
10 Correct 441 ms 8424 KB Output is correct
11 Correct 366 ms 9088 KB Output is correct
12 Correct 618 ms 9216 KB Output is correct
13 Correct 592 ms 9336 KB Output is correct
14 Correct 524 ms 9336 KB Output is correct
15 Correct 631 ms 8924 KB Output is correct
16 Correct 342 ms 8972 KB Output is correct
17 Correct 532 ms 8832 KB Output is correct
18 Correct 534 ms 8960 KB Output is correct
19 Correct 458 ms 8952 KB Output is correct
20 Correct 550 ms 8748 KB Output is correct