"Algorithmic learning note" 33. Online algorithm SJTU OJ 1006 summation Game

Source: Internet
Author: User

1006. Summation Game Description

There is a row of stone keyboards on the stone pillars, with an integer on each key. Please select two keys on the keyboard to make the keys between the two keys and the number and maximum. If this is the largest and not positive, then output "Game over".

Input Format

Line 1th: Number of keys N.

2nd.. N+1: The numeric integer on the key ai.

−100≤ a I≤100

For 70% of data, 2< Span id= "mathjax-span-19" class= "Mo" >≤n ≤1 ,000

For 100% of data,2≤n≤1,

Output Format

row, maximum and or "Game over".

Sample Input
53-57-28
Sample Output
13
Sample Input
3-6-9-10
Sample Output
Game Over

This question is very similar to the previous article on columns, but it is different because it is an application problem ... It requires at least two keys to be in line, so the smallest subsequence is also guaranteed to be longer than or equal to 2.

So in this case, our original online algorithm cannot be used directly.

Here are two new online algorithms to complete this problem, one specifically for this problem, and one is a new approach to solve the original problem and the new problem.

New online Algorithm 1:
The core idea remains: a negative number is encountered that can be reset at the current and. However, in the driving process to take into account that the minimum length of 2 is always maintained, so the constant dislocation to complete the matter.
In the process of re-processing, we need to make the decision is that I must be continued, as for the head is to continue the last time, or its previous position only need to compare size.
If the n[i-1] is bigger than the current one, then it's good to start with it--if it's current and bigger, it's going to go on.
The code is as follows:
intMain () {inti,n,max,temp; CIN>>N; intNum[n];  for(i=0; i<n;++i) Cin>>Num[i]; Max= TEMP = num[0]+num[1];  for(i=2; i<n;++i) {//There are two kinds of results for each I either add it in or reset the current fragment to start with I-1Temp= temp > num[i-1] ?Temp+Num[i]: num[i-1] +Num[i]; //if temp > Num[i] then continue to num[i] it's definitely bigger.//If temp < Num[i] Then you can reset the resume from n[i-1. It must be bigger than before .        if(max<temp) Max=temp; }    if(max>0) cout<<Max; Elsecout<<"Game over"; return 0;}
View Code

The second method is this idea:

1. The and of a continuous sub-sequence = from the first number to the end of the sequence and-from the first number to the end of the sequence.

2. To maximize the left side of the equals sign, in each calculation just let the last item be minimized to

So we have to maintain smallest and then find the maximum value in all the curtotal-smallest;

But here's a very critical question: first, to maintain the minimum or to update the current sequence of successive sub-sequences first?

If the minimum value is maintained first, then the

if (Result < Total-smallest)
result = Total-smallest;

The smallest in the preceding i-1 is the sequential sub-column and the minimum value in the sequence of the number of

Then use total to subtract, the least you get is only one element (if the tail of smallest is i-1) is also a classic problem

What we require, however, is that the smallest must refer to the sequence of the number of previous i-2 and the minimum, so that there are at least two elements after subtraction. Which is the application problem.

To achieve this, we need to use a technique that is the post-Update method, which puts the smallest update in the back of the above sentence so that it is actually the minimum value to i-2 until it is called.

The code is as follows:

intMain () {intNum0); CIN>>num; intResult0); intTotal0), Smallest (32785);//Smalest represents the continuous subsequence starting from the first number and the smallest one, and that is , the end of a certain number.//Total represents all the numbers from the beginning of the first number up to now, and that is, at the end of the current//The subtraction is the subsequence from the end of the smallest to the present and then uses result to maintain the maximum value .     for(inti =0, temp; i < num; ++i) {cin>>temp; Total+=temp; //result = max (result, total-smallest)//This is because smallest maintains the first 2 numbers.//then the result of subtracting must contain at least the previous number        if(Result < Total-smallest) result= Total-smallest; //smallest = min (smallest, total-temp)//the smallest maintained here is from the first number to the top 2 numbers of the current number.//because I hungry total-temp is actually to i-1 total        if(Smallest > Total-temp) Smallest= Total-temp; }    if(Result >0) cout << result <<Endl; Elsecout <<"Game over\n";}
View Code

Supplemental Links:

http://blog.csdn.net/hcbbt/article/details/10454947 six methods to solve the continuous sub-sequence of the largest and

Http://www.cnblogs.com/txd0u/p/3353355.html 1006

"Algorithmic learning note" 33. Online algorithm SJTU OJ 1006 summation Game

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.