# [BZOJ1609][Usaco2008 Feb]Eating Together麻烦的聚餐

## 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐

Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 1653  Solved: 1010 [Submit][Status][Discuss]

5
1
3
2
1
1

## Sample Output

1

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 30000 + 10;
int a[maxn], b[maxn];
int dp[maxn][4];
int main(){
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", a + i);
b[n - i + 1] = a[i];
}
dp[1][1] = dp[1][2] = dp[1][3] = 1;
dp[1][a[1]] = 0;
for(int num, i = 2; i <= n; i++){
num = dp[i - 1][1];
for(int j = 1; j <= 3; j++){
dp[i][j] = num + (a[i] != j);
num = min(num, dp[i - 1][j + 1]);
}
}
int ans = 66662333;
for(int i = 1; i <= 3; i++)
ans = min(ans, dp[n][i]);
dp[1][1] = dp[1][2] = dp[1][3] = 1;
dp[1][b[1]] = 0;
for(int num, i = 2; i <= n; i++){
num = dp[i - 1][1];
for(int j = 1; j <= 3; j++){
dp[i][j] = num + (b[i] != j);
num = min(num, dp[i - 1][j + 1]);
}
}
for(int i = 1; i <= 3; i++)
ans = min(ans, dp[n][i]);
printf("%d\n", ans);
return 0;
}

posted @ 2017-09-04 19:05  Elder_Giang  阅读(129)  评论(0编辑  收藏  举报