版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/。未经本作者同意不得转载。 https://blog.csdn.net/kenden23/article/details/24862445

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

本题由于数据量小,能够使用暴力法,时间效率是O(n^3)

可是这里巧用最大子段和的思想。能够把时间效率降到O(n)

思想:

1 想使用一个新的数列,计算连续出现了多少个1和连续出现了多少个零

2 求这个新数列的最大子段和

3 Flip最大子段中的 0 和 1,

4 计算出结果

比暴力法复杂非常多了,可是时间效率却提高了三个档次。

#include <vector>
#include <string>
#include <iostream>
using namespace std; void FlippingGame()
{
int n, a;
cin>>n;
vector<bool> vbn(n);
for (int i = 0; i < n; i++)
{
cin>>a;
vbn[i] = a;
}
vector<int> ans;
int c = 1;
for (int i = 1; i < n; i++)
{
if (vbn[i] == vbn[i-1]) c++;
else
{
if (vbn[i-1]) ans.push_back(-c);
else ans.push_back(c);
c = 1;
}
}
if (vbn.back()) ans.push_back(-c);
else ans.push_back(c); //求最大子段和思想
int stTmp = 0, st = ans.size(), end = ans.size(), maxVal = 0, sum = 0;
for (unsigned i = 0; i < ans.size(); i++)
{
sum += ans[i];
if (sum > maxVal)
{
st = stTmp;
maxVal = sum;
end = i;
}
if (sum <= 0)
{
sum = 0;
stTmp = i+1;
}
} int nums = 0;
for (int i = 0; i < ans.size(); i++)
{
if (ans[i] < 0) nums += ans[i];
}
if (maxVal > 0) cout<<maxVal - nums;
else cout<<-(ans.front()+1);
}

codeforces Flipping Game 题解的更多相关文章

  1. Codeforces Round #556 题解

    Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...

  2. Codeforces Round #569 题解

    Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...

  3. Codeforces Round #557 题解【更完了】

    Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...

  4. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  5. codeforces CF475 ABC 题解

    Bayan 2015 Contest Warm Up http://codeforces.com/contest/475 A - Bayan Bus B - Strongly Connected Ci ...

  6. Codeforces Round #542 题解

    Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...

  7. Codeforces Choosing Laptop 题解

    这题实在是太水了,具体看注释 蒟蒻的方法是一边找过时的电脑一边比大小 蒟蒻不才,只会C++ 其实还会free basic,但它已经过时了 附: 本题洛谷网址 Codeforces网址 希望蒟蒻的题解能 ...

  8. Codeforces Flipping game 动态规划基础

    题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...

  9. Codeforces 381 简要题解

    做的太糟糕了...第一题看成两人都取最优策略,写了个n^2的dp,还好pre-test良心(感觉TC和CF的pretest还是很靠谱的),让我反复过不去,仔细看题原来是取两边最大的啊!!!前30分钟就 ...

随机推荐

  1. 在Xcode中手动添加pch文件

    在Xcode中手动添加pch文件: 一: 在工程中新建.pch文件,pch文件名通常用工程名字命名: 二: 在Targets->build Settings->Prefix Header ...

  2. 使用webgl(three.js)创建3D机房(升级版)-普通机房

    序: 目前市面上的数据中心主要分两大类,一类属于普通数据中心,机柜按照XY轴 有序排放,一类属于微模块集合的数据中心,多个机柜组合而成的微模块.  本节课主要详细讲解普通数据中心的可视化展示,浏览器直 ...

  3. 测试域名ping延迟脚本

    #!/bin/bash if [ $# -lt 1 ]thenecho "Usage:avg file1"exit 1fiecho "================== ...

  4. Expression Blend实例中文教程(3) - 布局控件快速入门Grid

    上一篇对Blend 3开发界面进行了快速入门介绍,本篇将基于Blend 3介绍Silverlight控件.对于微软开发工具熟悉的朋友,相信您很快就熟悉Blend的开发界面和控件. XAML概述 Sil ...

  5. input的属性用法介绍

    Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍. 1,type=text 输入类型是text,这是我们见的 ...

  6. JavaScript之parseInt()数值转换常被忽略的问题

    使用parseInt()你可以从字符串中获取数值,该方法接受另一个基数参数,这经常省略,但不应该.当字符串以”0″开头的时候就有可能会出问题,例如,部分时间进入表单域,在ECMAScript 3中,开 ...

  7. Js获取request中的对象的属相值

    将这个值放在页面上.再取出来. 在body中定义隐藏属性的input按钮来接收request中的值: <input type="hidden" value="< ...

  8. 固态硬盘SSD与闪存(Flash Memory)

    转自:http://qiaodahai.com/solid-state-drives-ssd-and-flash-memory.html 固态硬盘SSD(Solid State Drive)泛指使用N ...

  9. poj 2392 建塔(多重背包+不定上界)

    http://blog.csdn.net/libin56842/article/details/9492351 这次比较理解那个!dp[j]是为了什么,因为是滚动数组,没有这个的话used那边会出问题 ...

  10. 《深入理解Java内存模型》读书总结(转-总结很好)

    概要 文章是<深入理解Java内容模型>读书笔记,该书总共包括了3部分的知识. 第1部分,基本概念 包括“并发.同步.主内存.本地内存.重排序.内存屏障.happens before规则. ...