Codeforces Flipping game 动态规划基础

题目链接:http://codeforces.com/problemset/problem/327/A

这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N)

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <cctype>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 #include <queue>
10 #include <list>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 int a[102], b[102], c[102];
15 int main ( void )
16 {
17     int n, n1=0, cnt=0; 
18     while (~scanf("%d", &n))
19     {
20         n1 = cnt = 0;
21         memset(a, 0, sizeof(a));
22         memset(b, 0, sizeof(b));
23         for (int i=1; i<=n; ++i){
24             scanf("%d",a+i); if(a[i]) n1++;
25             if (a[i]) b[i]=-1; else b[i]=1;
26         }
27         c[0] = 0;
28         for (int i = 1; i <= n; ++i)
29         {
30             c[i] = c[i-1] + b[i];
31         }
32         int Max = -INF, Min = c[0];
33         for (int i = 1; i <= n; ++i) {
34             if (c[i] - Min > Max) Max = c[i]-Min;
35             if (c[i] < Min) Min = c[i];
36         }
37         printf("%d\n",n1+Max);
38     }
39         return 0;
40 }            

转化为子序列的最大连续和

posted on 2013-07-19 01:05  aries__liu  阅读(268)  评论(0编辑  收藏  举报