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 xmeans 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
题意:翻牌游戏。

给出n张牌,每张牌仅仅有0和1两种状态。给出初始状态。对于翻牌操作这样规定:每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0)。求一次翻转操作后,1的个数尽量多。
枚举区间+遍历区间推断,O(n^3);
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int INF=0x3f3f3f3f;
#define LL long long
int a[110];
int main()
{
int n,num[2];
while(~scanf("%d",&n))
{
int ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
if(a[i]==1)
ans++;
}
int pos=ans;
if(pos==n)
{
printf("%d\n",n-1);
continue;
}
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
{
memset(num,0,sizeof(num));
for(int k=i;k<=j;k++)
num[a[k]]++;
if(num[0]>num[1])
ans=max(ans,pos+num[0]-num[1]);
}
printf("%d\n",ans);
}
return 0;
}

Codeforces 327A-Flipping Game(暴力枚举)的更多相关文章

  1. Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  2. codeforces Restore Cube(暴力枚举)

    /* 题意:给出立方体的每个顶点的坐标(是由源坐标三个数某几个数被交换之后得到的!), 问是否可以还原出一个立方体的坐标,注意这一句话: The numbers in the i-th output ...

  3. codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)

    题意:n个点,m条无向边,每个边有权值,给你 s 和 t,问你至多删除两条边,让s,t不连通,问方案的权值和最小为多少,并且输出删的边 分析:n<=1000,m是30000  s,t有4种情况( ...

  4. Diverse Garland CodeForces - 1108D (贪心+暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  5. CodeForces 496D Tennis Game (暴力枚举)

    题意:进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时, 游戏结束.现在给出n次对决的记录,问可能的s和t有多少种,并按s递增 ...

  6. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  7. Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举

    B. Covered Path Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...

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

  9. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

随机推荐

  1. eclipse的springMVC环境搭建并输出HelloWorld

    spring简单介绍:https://www.cnblogs.com/package-java/p/10368672.html 1.创建一个Maven Project项目 点击下一步 点击下一步 2. ...

  2. Spark脚本调用

    Spark提供了多个脚本来作为程序的入口,其中最常用的是交互脚本 spark-shell, pyspark,还有spark sql的客户端spark-sql. 这些脚本最后都会归结到对SparkSub ...

  3. 【BZOJ 1257】[CQOI2007]余数之和

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] k%i=k-(k/i)i 则∑k%i = nk-∑(k/i)*i 因为k/i是整除运算. 所以会有某一段连续的i,它们的k/i的值都 ...

  4. Python学习简单练习-99乘法表

    __author__ = 'ZFH'#-*- coding:utf-8 -*-for i in range(10): #外层循环,range(10),1-9 for j in range(1,i+1) ...

  5. LightOJ - 1038 Race to 1 Again 递推+期望

    题目大意:给出一个数,要求你按一定的规则将这个数变成1 规则例如以下,如果该数为D,要求你在[1,D]之间选出D的因子.用D除上这个因子,然后继续按该规则运算.直到该数变成1 问变成1的期望步数是多少 ...

  6. java学习记录笔记--继承,super,Object类

    继承: Java中的继承是单继承的. 1.子类拥有父类的全部属性和方法. 可是属性和方法的修饰符不能使private. 2.能够复用父类的代码. 方法的重写须要满足的条件: a.返回值类型 b.方法名 ...

  7. JAVA学习第四十五课 — 其它对象API(一)System、Runtime、Math类

    一.System类 1. static long currentTimeMillis() 返回以毫秒为单位的当前时间. 实际上:当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫 ...

  8. vijos--P1211--生日日数(纯模拟)

    P1211生日日数 未递交 标签:[显示标签] 描述 CCC老师的生日是YY年MM月DD日,他想知道自己出生后第一万天纪念日的日期(出生日算第0天). 格式 输入格式 从文件的第一行分别读入YY,MM ...

  9. 版本控制器:SVN(精讲)

    版本控制器:SVN 1 开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目 ...

  10. for循环练习-----ATM取款

    要求: 代码: package com.jianglai.atm; import java.util.Scanner; public class ATM { public static void ma ...