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
n integers: 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.

Sample test(s)
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.

题意:有n张牌,仅仅有0和1,问在[i,j]范围内翻转一次使1的数量最多。

输出1最多的牌的数量

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int n,i,j,k,t;
int a[110];
int sum[2];
int cnt=0;
while(~scanf("%d",&n))
{
cnt=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
cnt++;//记录開始时1的牌数
}
t=cnt;
if(cnt==n)
{
printf("%d\n",n-1);//假设全是1的话 你得翻一张牌 所以剩下的最大数为总数-1
}
else
{ for(i=0; i<n; i++)
for(j=i; j<n; j++)
{
memset(sum,0,sizeof(sum));
for(k=i; k<=j; k++)
sum[a[k]]++;
if(sum[0]>sum[1])
{
if(cnt<t+sum[0]-sum[1])
{
cnt=t+sum[0]-sum[1];
}
}
}
printf("%d\n",cnt);
}
}
return 0;
}



Flipping Game(枚举)的更多相关文章

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

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

  2. UVALive 3953 Strange Billboard (状态压缩+枚举)

    Strange Billboard 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/A Description The marke ...

  3. 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 ...

  4. POJ:3185-The Water Bowls(枚举反转)

    The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7402 Accepted: 2927 Descr ...

  5. Codeforces 327A-Flipping Game(暴力枚举)

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

  6. ZOJ - 4114 Flipping Game

    ZOJ - 4114 Flipping Game 题目大意:给出两个串s,t,n个灯泡的序列,1代表开着,0代表关着,一共操作k轮,每轮改变m个灯泡的状态,问最终能把s串变成t串的方案数. 坤神题解. ...

  7. Swift enum(枚举)使用范例

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  8. 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)

    建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...

  9. Objective-C枚举的几种定义方式与使用

    假设我们需要表示网络连接状态,可以用下列枚举表示: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateC ...

随机推荐

  1. Mac OS设置rootpassword

    不知怎么忘记mac os系统的rootpassword了 方法一: 首先,启动机器,启动时按住Apple和S键,以单用户模式(single user mode)进入系统.输入:mount -uw / ...

  2. net析构函数对垃圾回收的影响

    net析构函数对垃圾回收的影响 之前忘了说了 代码都是在Release模式下运行的,现在补充上. 这里说析构函数,其实并不准确,应该叫Finalize函数,Finalize函数形式上和c++的析构函数 ...

  3. Bootstrap,Foundation和TypeScript

    http://www.oschina.net/question/12_128155 http://www.oschina.net/news/72330/typescript-2-0 给自己提个醒,随时 ...

  4. 【Dev Club 分享】腾讯验证码的十二年

    源:http://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653578147&idx=3&sn=94a8f8f8b4a23 ...

  5. Visual Studio 控件命名规范(很详细)

    VS 控件命名规范 Type Prefix Example Array arr arrShoppingList Boolean bln blnIsPostBack Byte byt bytPixelV ...

  6. [Usaco2008 Feb]Meteor Shower流星雨

    去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的一切东西造成毁灭性的打击 ...

  7. HDU 3478 Play with Chain (Splay树)

    这种高级数据结构太难搞了.........现在还是先照着别人的代码敲,做模板..........慢慢花时间来弄懂 #include <iostream> #include <algo ...

  8. Swift - 类的计算属性(使用get和set来间接获取/改变其他属性的值)

    1,Swift中类可以使用计算属性,即使用get和set来间接获取/改变其他属性的值,代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Calc ...

  9. C语言中输入输出重定,freopen()妙用。

    使用的理由(范围):如果输入数据很庞大,需要一次又一次的重新输入和调试时可采用本函数. freopen ()函数: 1.格式 FILE * freopen ( const char * filenam ...

  10. dll导入导出资源文件查看工具 InspectExe

    InspectExe lets you explore and diagnose problems with Win32 applications. It is integrated directly ...