版权声明:本文作者靖心,靖空间地址: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. String.Compare 方法 (String, Int32, String, Int32, Int32)

    String.Compare 方法 (String, Int32, String, Int32, Int32) 对两个指定的 String 对象的子字符串进行比较,并返回一个指示二者在排序顺序中的相对 ...

  2. AngularJS的基础知识

    一.AngularJS指令与表达式 [AngularJS常用指令]1.ng-app:声明Angular所管辖的区域,一般写在body或HTML上,原则上一个页面只有一个.2.ng-model:把元素值 ...

  3. 读EntityFramework.DynamicFilters源码_心得_单元测试03

    上个星期我们只是显示了一个示例,怎么在EF的框架内,注入我们拓展的动态过滤器 第一步:安装EntityFramework.DynamicFilters 第二步:重写OnModelCreating方法 ...

  4. pycharm下 os.system os.popen执行命令返回有中文乱码

    原文 settings:

  5. mysql 查询近几天的结果

    //近两天的 不包括当天的数据 select * from order_info 今天的数据 select* fromorder_info where date(createtime)=curdate ...

  6. 模块与包&常用模块

    一.模块的使用 模块定义:一系列功能的集合体 分为三大类:1.自定义模块 2.内置模块(比如 time,os,sys) 3.第三方模块 模块的表现形式: 1.使用python编写的py文件 2.已被编 ...

  7. csharp: Converting chinese character to Unicode

    Function chinese2unicode(Str) Dim Str_one:Str_one = "" Dim Str_unicode:Str_unicode = " ...

  8. csharp:Convert Image to Base64 String and Base64 String to Image

    /// <summary> /// 图像转成二进制数组 /// </summary> /// <param name="imageIn">< ...

  9. mybatis整合spring的时候配置数据库信息文件properties注意事项

    信息后面不能有空格 ,格式要xxx.driver xxx.url 这样

  10. atoi、itoa,strcpy,strcmp,memcpy等实现

    原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http ...