Fuzhou University 14th Session Program Design Competition _ Replay A Salty Fish

Source: Internet
Author: User

The seaside lies a row of salted fish, some have the dream of salted fish successfully turned over (however no egg use), some is to continue when salted fish. A kind fisherman wants to help these salted fish to turn over, but the fisherman is more lazy, so will only start from a certain salted fish, toward a direction, a only salted fish over the past, flipping a few only then turned away, deep work and name. More precisely, fishermen will choose an interval [l,r], change the state of all salted fish in the range, at least flip a salted fish.

After the fisherman left to know if he took the best strategy, the maximum number of salted fish successfully turned over, but there are about 100,000 salted fish, so this problem will be handed over to you.

Input

Contains multiple sets of test data.

The first behavior of each group of test data is positive integer n, which indicates the number of salted fish.

The second act is 01 strings long N, 0 means no roll over, and 1 indicates a successful roll-over.

n≤100000

Output

After the fisherman's operation, the maximum number of salted fish (ie, 1) was successfully rolled over.

Sample Input

5

1 0 0) 1 0

3

0 1 0

Sample Output

4

2

Hint

For the first example, the flip interval [2,3], the sequence becomes 1 1 1 1 0.

For the second example, flip the entire interval and the sequence becomes 1 0 1.

Ideas: For the success of the salted fish to turn over is a successful turning over the total number of salted fish-1, for the non-roll over the salted fish is a successful turn over the total number of salted fish +1, so open a C array, so that there is no turning over the salted fish for 1, roll over for 1, the largest sub-section of C The result plus the number of salted fish that turned out to be successful is the answer.


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h >
using namespace std;
int a[100005],c[100005];
int main ()
{
	int n,sum;
	while (~SCANF ("%d", &n))
	{
		sum=0;
		int s[100005]={0};
		int mx=0;
		for (int i=1;i<=n;i++)
		{
			scanf ("%d", &a[i]);
			if (a[i]==1)
			{
				c[i]=-1;
				sum++;
			}
			else
			c[i]=1; 
			 
            S[i]=max (S[i-1]+c[i],c[i]);     
            if (Mx<s[i])             
            {  
                mx=s[i];           
            }     
		}
		if (sum==n)
		mx=-1; 
		printf ("%d\n", sum+mx);
	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.