Ant Puzzle (ii) Maximum Subarray and (DP) for the end-to-end array

Source: Internet
Author: User

The problem of Ants (ii) time limit:MS | Memory limit:65535 KB Difficulty:3
Describe

It rained, it rained, the ants moved.

It is known that n kinds of ingredients need to be removed, and these ingredients form a circle from 1 to N in turn. The little ant has a value VI for each of the ingredients, of course, if VI is less than 0, it means the ant hates the ingredients. Because it is going to rain soon, so ants can only move once, but can remove a continuous section of the ingredients. Time is urgent, you can help small ants, let it move away from the ingredients love value and maximum.

Input
There are several sets of test data (end with EOF).
There are two rows for each set of data, and the first row has an N, indicating that there are n kinds of ingredients lined up in a circle. (1 <= n<= 50000)
The second row has n numbers, which represent the ant's favorite value VI for the nth type of food. ( -10^9 <= Vi <= 10^9)
Output
The
maximum value of the sum of the favorite values of the ingredients that the small ant can move out.
Sample input
33-1 25-8 5-1 3-9
Sample output
57

The problem is the largest field and the deformation, the key point is that the data can be recycled in two steps to solve:

The first step is to find out the maximum field and S1 of this set of data.

The second step, this can be in the first step, to find his minimum field and S2, as well as the sum of all the data s, then, according to the requirements of the topic, the S1 and s-s2 to compare the value of the final result can be obtained.

#include <stdio.h>
#define MAX (x, y) x>y?x:y
#define MIN (x, y) x>y?y:x
Long Long arr[200001];
int main () {Long long n,temp1,max,min,s,temp2,result;
while (~SCANF ("%lld", &n)) {
for (int i=0;i<n;++i) scanf ("%lld", &arr[i]);
for (int i=n,j=0;i<2*n;++i,j++) ARR[I]=ARR[J];
temp1=max=-1000;min=temp2=1000;s=0;
for (int i=0;i<n;++i) {
if (Temp1+arr[i]<arr[i]) temp1=arr[i];
else Temp1+=arr[i];
Max=max (MAX,TEMP1);
if (Temp2+arr[i]>arr[i]) temp2=arr[i];
else Temp2+=arr[i];
Min=min (MIN,TEMP2);
S+=arr[i];
printf ("t=%d temp=%d\n", t,temp);
Result=max (S-min,max);
}
if (result<0) result=0;
printf ("%lld\n", result);}
return 0;
}

Maximum subarray and time limit for the concatenated array: MS | Memory limit:65535 KB Difficulty: 4
Describe
given an array of n integer elements arr, there are positive and negative numbers in the array, this array is not a normal array, and its end-to-end is connected. One or more contiguous elements in an array can form a sub-array, where such a subarray exists arr[i],... arr[n-1],arr[0],..., arr[j], and now you have this acm_lover One of the most efficient ways to help find out the maximum value of all successive sub-arrays (if the elements in the array are all negative, the maximum is 0, which is not selected).
Input
The input contains multiple test cases, each with two rows, the first line being an integer n (1=<n<=100000), the length of the array, and the second line entering n integers (the absolute value is not greater than 1000).
Output
For each test case, output the maximum value of the sub-array and.
Sample input
61-2 3 5-1 256-1 5 4-7
Sample output
1014
Two-question code like ...

Ant Puzzle (ii) Maximum Subarray and (DP) for the end-to-end array

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.