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. 开发人员福利!ChromeSnifferPlus 插件正式登陆 Chrome Web Store

    今天(2014-10-30)下午,ChromeSnifferPlus 插件正式登陆 Chrome Web Store. 在线安装地址: https://chrome.google.com/websto ...

  2. Processing.js

    Processing.js Processing.js 1.4.1 released!

  3. 得到一个div下 特定ID的所有标签

    比如说得到 <div id="showsp"> <div id="a"></div> <div id="a& ...

  4. poj 3450 Corporate Identity

    题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...

  5. c 可变参数 定义可变参数的函数

    定义可变参数的函数,需要在stdarg.h头文件中定义的va_list类型和va_start.va_arg.va_end三个宏. 定义可变参数函数 va_list ap;  //实际是定义一个指针va ...

  6. 获取Jenkins project build结果

    当Jenkins管理的build project越来越多的时候,须要脚本收集每一个project的近期一次build结果,从而集中管理.依据业务规则,决定是否重算和何时重算. 以下的命令是利用curl ...

  7. php 简易验证码(GD库)

    论坛中为了防止灌水,出现了非常多的验证码的插件.如今这里介绍一个非常easy的自己定义验证码函数,这个验证码实现的原理就是通过php扩展的gd库来实现的. 给出百度百科对验证码的定义"验证码 ...

  8. UNIX 网络编程之线程

    概述: 实现并发服务器一般都是父进程accept一个连接,然后fork一个子进程,该子进程处理与该连接对端的客户之间的通信.但是fork是昂贵,耗资源和时间.而线程是轻量级线程,它的创建比进程的创建块 ...

  9. cocos2d-x3.2下使用Umeng 64位SDK注意事项

    友盟官方的样例中已经有了Cocos2d-x的Demo使用起来也是比較方便的.但在64位的版本号使用时须要注意 32位SDK改动: 将Xcode中Build Settings的Architectures ...

  10. 公布windows的&quot;Universal Apps&quot; Unity3D游戏

    转载请注明出处:http://blog.csdn.net/u010019717 更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/space-uid-18602.h ...