版权声明:本文作者靖心,靖空间地址: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. mybatis学习之高级映射

    一对一映射查询 1.数据库执行脚本: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.7.11-log : Database - db_mybatis *** ...

  2. Java基础(三)选择和循环结构

    一.选择结构,条件判断 1.if 语句 一个 if 语句包含一个布尔表达式和一条或多条语句.如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代码. impor ...

  3. hibernate下Session的获取方式

    Session是应用程序与数据库之间的一个会话,其重要性不言而喻.初学Hibernate,使用SessionFactory,老老实实地打开事务,提交,回滚,关闭session. 1.直接通过Sessi ...

  4. webpack build后生成的app、vendor、manifest三者有何职能不同?

    贴一下之前vue脚手架的webpack3配置: app.js是入口js,vendor则是通过提取公共模块插件来提取的代码块(webpack本身带的模块化代码部分),而manifest则是在vendor ...

  5. Docker 简单运用

    Docker 帮助系统管理员和程序员在容器中开发应用程序,并且可以扩展到成千上万的节点,容器和 VM(虚拟机)的主要区别是,容器提供了基于进程的隔离,而虚拟机提供了资源的完全隔离.虚拟机可能需要一分钟 ...

  6. MySQL创建子视图并查看的时候,字符集报错问题

    进入命令行模式, 如果MySQL数据库已经安装好,可以使用下列SQL命令查看MySQL当前的字符集设置: mysql> show variables like 'character_set_%' ...

  7. <Android 应用 之路> MPAndroidChart~LineChart

    简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...

  8. DELPHI 小结

    //十六进制(S)-->>十进制(I)  [重写:Jey]function hextoint(s: string): Integer; begin           //$代表16进制  ...

  9. LearnHowToThink

    一.BubbleSort and XListview 1.BubbleSort (1)analysis traverse.compare.exchange.cycle.optimize strateg ...

  10. Maven库下载很慢解决办法,利用中央仓库

    以下四个都是可用的: http://mirrors.ibiblio.org/maven2/ http://mvnrepository.com/ http://repository.jboss.org/ ...