Kadane Algorithm

摘要:
KadaneAlgorithm找到数组中连续子序列的最大和。当1的返回值为-1.0时,我们计算序列的最大返回值,也就是说,序列的最大子序列和最大返回值加上原始1的数量就是答案。

Kadane Algorithm

  求一个数组里连续子序列的和最大。

for(int i=1;i<=n;i++)
    {
        nmax+=a[i];
        maxx=max(maxx,nmax);
        nmax=max(nmax,0);
    }
A. Flipping Game

Description

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

Examples

Input

5
1 0 0 1 0

Output

4

正确解法:

一直在想O(n)怎么写

题目是说 把一个子序列翻转,求最大1的个数。

我们把1的收益为 -1,0的收益为 1

求一个序列的最大收益,也就是求这个序列的最大子序列和

最大收益加上原本1的个数就是答案了。

Kadane Algorithm第1张Kadane Algorithm第2张
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 3000000+10;
 5 const ll inf = 1e17;
 6 int n;
 7 int a[150],yi,maxx=-999999,nmax;
 8 int main()
 9 {
10     scanf("%d",&n);
11     for(int i=1;i<=n;i++)
12     {
13         scanf("%d",&a[i]);
14         if(a[i]==1)
15         {
16             yi++;
17             a[i]=-1;
18         }
19         else
20             a[i]=1;
21     }
22     for(int i=1;i<=n;i++)
23     {
24         nmax+=a[i];
25         maxx=max(maxx,nmax);
26         nmax=max(nmax,0);
27     }
28     printf("%d
",maxx+yi);
29 
30     return 0;
31 }
View Code

免责声明:文章转载自《Kadane Algorithm》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇janusgraph-图数据库的学习(1)Markdown简单使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

JS事件 文本框内容改变事件(onchange)通过改变文本框的内容来触发onchange事件,同时执行被调用的程序。

以下代码显示,当用户更改文本框中的文本时,会弹出一个对话框“您更改了文本内容!”。运行结果:该任务补充了右侧编辑器的第13行。当文本框的内容发生更改时,将调用message()函数,并弹出对话框“您更改了文本内容!”。DOCTYPEHTML˃文本框内容更改事件functionmessage(){alert(“您更改了文本内容!”);}个人简介:请编写您的个人...

【使用 DOM】为DOM元素设置样式

DOCTYPE html˃设置DOM元素的样式p{border:中双绿色;背景颜色:浅灰色;}#block1{color:白色;}table{border:thinsolided;border collapse:collapse;margin:5px;float:left;}td{padding:2px;}#block2{color:yellow;font-...

解决Maven无法下载fastdfs-client-java依赖,Dependency 'org.csource:fastdfs-client-java:1.27-SNAPSHOT' not found.

然后,您成功地将fastdfs客户端java打包到本地Maven仓库,更新项目Maven,pom.xml文件将不会找不到fastdfs客户机java依赖项。...

boost的下载和安装(windows版)

1 Introduction boost是一个准C++标准库,相当于STL的延续和扩展。它的设计理念类似于STL,它使用泛型来最大化重用。对于2boost的下载和安装,我们可以在官方boost网站上下载最新的boost版本。因为boost库可以像标准库一样在多个平台上运行,所以它只以源代码的形式正式提供。这是因为boost windows的安装版本不仅与处理...

华为交换机堆叠配置

请参考华为交换机的配置堆栈。[Leaf1-stack-port0/1]portinterfaceg0/0/12启用物理接口12加入堆栈组[Leaf1]stackslot0priority255修改优先级255,默认值为100警告:不要频繁修改优先级,因为它会使堆栈分裂。持续...

SqlServer数据库存入decimal类型数据注意事项

对于sqlserver,Decimal可用于存储具有小数点和固定值的值。与浮点和实数不同,十进制用于存储近似值。目的是满足精确数学运算的需要。它是最大和最精确的浮点数字类型。对于十进制类型,请注意必须指定精度;否则,十进制只能存储为整数,就像int一样。例如,十进制是存储长度为18位和小数点后2位的数据。...