A. Flipping Game
 
 
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Iahub got bored, so he invented a game to be played on paper.

He writes n integers a1, a2, ..., an. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1 ≤ i ≤ j ≤ n) and flips all values ak for which their positions are in range [i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 1 - x.

The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 100). In the second line of the input there are nintegers: a1, a2, ..., an. It is guaranteed that each of those n values is either 0 or 1.

Output

Print an integer — the maximal number of 1s that can be obtained after exactly one move.

Examples
input
5
1 0 0 1 0
output
4
input
4
1 0 0 1
output
4
Note

In the first case, flip the segment from 2 to 5 (i = 2, j = 5). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].

In the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1.

【题意】:题意是输入一个只有0和1的序列,要求找出一个合理的区间,在这个区间里面把0变成1,1变成0,使得该区间和该区间外的1的个数达到最大。

【分析】:暴力,遍历每个区间段,小区间内的1个数 = 小区间长度 - 小区间内1的个数。小区间外1个数 = 大区间1个数 - 小区间内1个数。然后每次更新。

dp,就是求出最大区间0的个数(这个区间中1的影响为-1,0的影响为1),然后加上所有1个数就是最终答案了。

【代码】:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std; const int maxn = +; int main()
{
int n, a[maxn], i, j, k, count1, count0, max, t0, t1, t;
while (cin >> n)
{
for (t = i = ; i < n; i++)
{
scanf("%d", &a[i]);
if (a[i])
t++; // 统计整个序列中1的个数 }
max = -;
for (i = ; i < n; i++)
{
for (j = i; j < n; j++)
{
count1 = count0 = ;
for (k = i; k <= j; k++) // 暴搜,统计每个区间的0、1数目
{
if (a[k]==)
count1++;
else
count0++;
}
if (max < count0 - count1) //max保存的是当前0、1数目最大的差(0最多,1最少)
{
max = count0 - count1;
}
}
}
printf("%d\n", t + max);
}
return ;
}

暴力枚举

#include <iostream>
#include <string>
#include <algorithm>
using namespace std; int main()
{
int n, x, mx = ;
cin >> n;
int cnt0 = , cnt1 = ;
for (int i = ; i < n; ++i)
{
cin >> x;
if (x == ) ++cnt1; //整个区间中1的个数
if (x == )
{
++cnt0;
if (cnt0 > mx) //翻转区间所得最多1的个数
mx = cnt0;
}
else if (cnt0) --cnt0; //1的值对cnt0的取值影响为-1
}
if (mx == ) --mx; //没有0,那么至少也要翻一次
cout << mx + cnt1 << '\n';
return ;
}

DP

Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】的更多相关文章

  1. 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game

    题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...

  2. Codeforces Round #191 (Div. 2)---A. Flipping Game

    Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Codeforces Round #191 (Div. 2) A. Flipping Game(简单)

    A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的 ...

  5. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  6. Codeforces Round #191 (Div. 2)

    在div 188中,幸运的达成了这学期的一个目标:CF1800+,所以这次可以打星去做div2,有点爽. A.Flipping Game 直接枚举 B. Hungry Sequence 由于素数的分布 ...

  7. Codeforces Round #191 (Div. 2) D. Block Tower

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  9. Codeforces Round #511 (Div. 1) C. Region Separation(dp + 数论)

    题意 一棵 \(n\) 个点的树,每个点有权值 \(a_i\) .你想砍树. 你可以砍任意次,每次你选择一些边断开,需要满足砍完后每个连通块的权值和是相等的.求有多少种砍树方案. \(n \le 10 ...

随机推荐

  1. 剑指Offer - 九度1387 - 斐波那契数列

    剑指Offer - 九度1387 - 斐波那契数列2013-11-24 03:08 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.斐波那契数列的定义如下: ...

  2. day06_05 字典

    1.0 字典 1.1 补充知识:用id可以查找出变量的内存地址 a = 10 print(id(a)) #找出内存地址 #>>>506528496 b = 15 print(id(b ...

  3. (转\整)UE4游戏优化 多人大地型游戏的优化(一)游戏线程的优化

    施主分享随缘,评论随心,@author:白袍小道 小道暗语: 1.因为小道这里博客目录没自己整,暂时就用随笔目录结构,所以二级目录那啥就忽略了.标题格式大致都是(原or转) 二级目录 (标题) 2.因 ...

  4. NVIDIA/DIGITS:Building DIGITS

    在 Prerequisites中的 sudo apt-get update命令发生错误: W: GPG 错误:http://developer.download.nvidia.com/compute/ ...

  5. linux备忘录-vi和vim

    知识点 vi的三种模式 一般模式 按 ESC 可回到一般模式 相关按键 j 代表 向下按钮 k 代表 向上按钮 h 代表 向左按钮 l 代表 向右按钮 20j 等代表 向下移动20行 Ctrl + f ...

  6. __PRETTY_FUNCTION__,__func__,__FUNCTION__

    今天在看苹果的官方demo的时候,发现这个打印调用方法的参数,很是好奇,遂bing了一番. NSLog(@"----------------%s",__PRETTY_FUNCTIO ...

  7. 软件架构---.net框架介绍

    https://www.cnblogs.com/hgmyz/p/5313983.html 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生 ...

  8. 使用common-fileUpload实现文件上传

    通过common-fileUpload文件上传组件,可以实现上传文档.图片等资料.根据程序的不同要求,它可以有多种方式的应用. 我们这里介绍一种简单的例子,来实现文件上传功能. 准备: 引入相关组建的 ...

  9. 使用Unity 实现ASP.NET Web API 依赖注入

    DI/IoC 的设计前面已经讲过好几次了,简单的一段话说明就是:「目标对象与外部相依的方式仅相依于 interface,而相依 interface 的 instance 透过 constructor ...

  10. [hdu6437]Problem L. Videos

    题目大意:有$n$个小时,有$m$个节目(每种节目都有类型$0/1$),有$k$个人,一个人连续看相同类型的节目会扣$w$快乐值. 每一种节目有都一个播放区间$[l,r]$.每个人同一时间只能看一个节 ...