版权声明:本文作者靖心,靖空间地址: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. 【转】C#中的委托,匿名方法和Lambda表达式

    简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...

  2. 架构实战项目心得(五):mysql安装

    1.  yum安装mysql yum -y install mysql-server 2.  启动mysql服务 启动mysql:service mysqld start 查看mysql的状态:ser ...

  3. [H5表单]html5自带表单验证体验优化及提示气泡修改

    慕课网之前录制的视频,js/jquery各种宽高的理解和应用,最近终于上线了.还有一个html5左侧导航没有上线!最近慕课网系列课程让我录制一个html5表单验证的课程.今天就稍微说一下表单验证!另外 ...

  4. c#中用DataView查询

    新人菜鸟 在开发一款软件,用到了 MyS中的 DataView 查询.查询后判断视图是否有返回值,找了好久,终于找到了他的 DataView XX.Count 参数. string table = S ...

  5. Nginx配置整理

    不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要 ...

  6. Azure 项目构建 – 部署高可用的 Python Web 应用

    Python 以其优美,清晰,简单的特性在全世界广泛流行,成为最主流的编程语言之一.Azure 平台针对 Python 提供了非常完备的支持.本项目中,您将了解如何构造和部署基于 Azure Web ...

  7. tr设置display属性时,在FF中td合并在第一个td中显示的问题

      今天用firefox测试页面的时候,发现用javascript控制 tr 的显示隐藏时,当把tr的显示由“display:none”改为“display:block”时,该tr下的td内容合并到了 ...

  8. 《Java并发编程实战》读书笔记(一)----- 简介

    简史 早期的计算机中不包含操作系统,从头至尾都只执行一个程序,并且这个程序能访问计算机所有资源.随着计算机发展,操作系统的出现,使得计算机可以同时运行多个程序,并且每程序都在单独的进程内运行.为什么要 ...

  9. JS读取粘贴板内容

    1.1     监听onpaste事件 1.1.1 定义和用法 npaste 事件在用户向元素中粘贴文本时触发. 注意: 虽然使用的 HTML 元素都支持 onpaste 事件,但实际上并非支持所有元 ...

  10. JSON对象的两个方法

    JSON对象有两个方法,stringify()和parse(). 最简单的方法,这两个方法分别用于吧JavaScript对象序列化为JSON字符串和把JSON字符串解析为原生JavaScript值. ...