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

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

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1646  Solved: 1005
[Submit][Status][Discuss]

5
1
3
2
1
1

1

Analysis

（然而我丧心病狂地去求最长升/降序，英勇送TLE）

Code

 1 #include<cstdio>
2 #include<iostream>
3 #define maxn 1000000
4 //using namespace std;
5
6 int min(int a,int b){return a<b?a:b;}
7 int min(int a,int b,int c){return min(min(a,b),c);}
8
9 int arr[maxn],DP[maxn][4],PD[maxn][4],n;
10
11 int main(){
12     scanf("%d",&n);
13
14     for(int i = 1;i <= n;i++){
15         scanf("%d",&arr[i]);
16
17         switch(arr[i]){
18             case 1:
19                 DP[i][1] = DP[i-1][1];
20                 DP[i][2] = min(DP[i-1][1],DP[i-1][2]+1);
21                 DP[i][3] = min(DP[i-1][1],DP[i-1][2]+1,DP[i-1][3]+1);
22                 break;
23             case 2:
24                 DP[i][1] = DP[i-1][1]+1;
25                 DP[i][2] = min(DP[i-1][1],DP[i-1][2]);
26                 DP[i][3] = min(DP[i-1][1],DP[i-1][2],DP[i-1][3]+1);
27                 break;
28             case 3:
29                 DP[i][1] = DP[i-1][1]+1;
30                 DP[i][2] = min(DP[i-1][1]+1,DP[i-1][2]+1);
31                 DP[i][3] = min(DP[i-1][1],DP[i-1][2],DP[i-1][3]);
32                 break;
33         }
34     }
35
36     for(int i = n;i >= 1;i--){
37         switch(arr[i]){
38             case 1:
39                 PD[i][1] = PD[i+1][1];
40                 PD[i][2] = min(PD[i+1][1],PD[i+1][2]+1);
41                 PD[i][3] = min(PD[i+1][1],PD[i+1][2]+1,PD[i+1][3]+1);
42                 break;
43             case 2:
44                 PD[i][1] = PD[i+1][1]+1;
45                 PD[i][2] = min(PD[i+1][1],PD[i+1][2]);
46                 PD[i][3] = min(PD[i+1][1],PD[i+1][2],PD[i+1][3]+1);
47                 break;
48             case 3:
49                 PD[i][1] = PD[i+1][1]+1;
50                 PD[i][2] = min(PD[i+1][1]+1,PD[i+1][2]+1);
51                 PD[i][3] = min(PD[i+1][1],PD[i+1][2],PD[i+1][3]);
52                 break;
53         }
54     }
55
56     int ans = min(min(PD[1][1],PD[1][2],PD[1][3]),min(DP[n][1],DP[n][2],DP[n][3]));
57
58     printf("%d",ans);
59
60     return 0;
61 }

posted @ 2017-08-31 19:16  Leviaton  阅读(106)  评论(0编辑  收藏  举报