1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
| #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int func(vector<int>& v) {
stack<P> st;
int n = v.size(), ret = 0;
rep(i, n) {
int len = 1, c = v[i];
REP(j, i+1, n) {
if(c == v[j]) {
i++;
len++;
continue;
} else break;
}
if(len >= 4) continue;
else {
if(st.size() == 0) {
st.push(mp(c, len));
ret += len;
} else {
P p = st.top();
if(p.first == c) {
if(p.second + len < 4) {
st.top().second += len;
ret += len;
} else {
st.pop();
ret -= p.second;
}
} else {
st.push(mp(c, len));
ret += len;
}
}
}
}
return ret;
}
int main() {
int n;
while(cin >> n && n) {
vector<int> v(n);
rep(i, n) cin >> v[i];
int ans = n + 1;
rep(i, n) {
int t = v[i];
v[i] = 1;
if(i == 0) {
v[i] = v[i+1];
ans = min(ans, func(v));
} else if(i == n-1) {
v[i] = v[i-1];
ans = min(ans, func(v));
} else {
if(v[i+1] == v[i-1]) {
v[i] = v[i+1];
ans = min(ans, func(v));
} else {
v[i] = v[i-1];
ans = min(ans, func(v));
v[i] = v[i+1];
ans = min(ans, func(v));
}
}
v[i] = t;
}
cout << ans << endl;
}
return 0;
}
|