codevs 2946 翻转游戏

2946 翻转游戏

 

题目描述 Description

现有n个数字a1,a2...an,其值均为0或1。

要求对着n个数中连续的若干个数进行一次取反(0->1,1->0),求所能得到的最多的1的数目。

输入描述 Input Description

第一行,n

第二行,n个数

输出描述 Output Description

能得到的最多的1的数目

样例输入 Sample Input
4
1 0 0 1
样例输出 Sample Output
4
数据范围及提示 Data Size & Hint

1 ≤ n ≤ 100

思路

枚举左右端点,同时记录下该区间内数字翻转后的值

代码

#include<cstdio>
#include<algorithm>
using namespace std;
bool a[101];
int n,ans,s,tot;
int main()
{
    int i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
      scanf("%d",&a[i]),ans+=a[i];
    for(i=1;i<=n;i++)
      for(j=i;j<=n;j++)
      {
          s=0;
          for(k=i;k<=j;k++)
        {
            if(a[k])
              s--;
            else
              s++;
        }
        tot=max(s,tot);
      }
    printf("%d",ans+tot);
    return 0;
        
          
}

 

posted @ 2016-10-18 20:27  一叶落尽天下秋  阅读(170)  评论(0编辑  收藏  举报