C language programming of 100 cases (13): the maximum sub-segment and

Example 13 sub-segment and the maximum

Title Description

Given a sequence, wherein the selected period of continuous and non-empty and that this maximum. For example, in the sequence 2, -4,3, -1,2, -4,3, the biggest sub-segment and 4, the sub-section 3, 1,2.

Input Format

The first line is a positive integer N, the length of the sequence.

The second line contains N absolute value is not greater than an integer of Ai 10000, this sequence is described.

Output Format

An integer, the largest sub-segment and how much. The minimum length of a subsegment.

SAMPLE INPUT

7

2 -4 3 -1 2 -4 3

Sample Output

4

        (1) programming ideas.

        Of length n from the leftmost column number (the array element is set to a [1]) to start scanning, up to the maximum sum of the rightmost (array element to a [n-1]) up, a note is encountered subsequence.

        Maxsum variables defined in the program to save the maximum continuous sub-section and, cursum save the current sub-segment and continuous.

        Initially, cursum = a [0], maxsum = a [0]. Circulating for (i = 1; i <n; i ++) for each element in the sequence a [i] for the scan process.

        In this scanning process, records left to right and the current sub-sequence (i.e. cursum = cursum + a [i]), and if this increased (i.e., currently a [i] is positive, so that cursum + a [ i]> maxsum possible), and then the maximum subsequence maxsum also increases, thereby updating maxsum. If the right scanning encountered negative, then the current subsequence cursum decreases and, at this time will be less than cursum maxsum, maxsum also not updated; while scanning the a [i], when cursum to 0, described that period has already been scanned will be abandoned, then the need to cursum set to zero. With this, the period after the cursum i will be analyzed, if larger than the current maxsum sub-segment needs to be updated maxsum. After such a trip scanning is finished, you can get the right result.

        (2) source.

#include <stdio.h>

int main ()

{

   int a[200001];

   int n, i Maxsum course;

   scanf("%d",&n);

   for (i=0;i<n;i++)

       scanf("%d",&a[i]);

   course = a [0];

   Special = a [0];

   for (i=1;i<n;i++)

   {

       if (cursum+a[i]>maxsum)

       {

             Maxsum course + = a [i];

       }

       if (cursum+a[i]<0)

       {

            course = 0;

       }

       else

          course course + = a [i];

   }

   printf("%d\n",maxsum);

   return 0;

}

Exercise 13

13-1 maximum difference

        This question is selected from the Los Valley exam (https://www.luogu.org/problem/P5146)

Title Description

HKE recent keen to study sequence, and once he found an interesting question:

For a sequence A1, A2 ⋯ An, find the two numbers i, j, 1≤i <j≤n, such that Aj -Ai maximum.

Now given this sequence, find the maximum value of Aj -Ai.

Input Format

The first line a positive integer n.

Then n integers, the first integer k + 1 to Ak.

Output Format

A maximum of behavior (Aj -Ai) of

SAMPLE INPUT

10

1 3 4 6 7 9 10 1 2 9

Sample Output

9

        (1) programming ideas.

        Since Aj -Ai seeking maximum requirement is the index of j> i, that is to say after the maximum number is the smallest number consistent with the requirements, it can not easily be found a sequence of a maximum number and minimum number max min ( We can not guarantee a certain max min later), and then output as the max-min answers.

        Minnum defined variables stored in the minimum number of sequences, the maximum difference MaxDiff saved, since the input n> = 2, it is possible to enter the first two sequence numbers, a and b, the initial value is assigned minnum small number of a and b, maxdiff initial value of ba.

        Then circulating for (i = 3; i <= n; i ++) For each element Ai in the scanning process sequence. Processing, if the difference between the current Ai is greater than the minimum number minnum MaxDiff, MaxDiff is updated, the update must be effective, in that it holds a certain minimum number minnum prior to the current number of Ai; Ai if the current number is less than the minimum number, The minimum number of minnum update to the current number of Ai.

        After such a trip scanning is finished, you can get the right result.

       (2) source.

#include <stdio.h>

int main ()

{

    int mention maxdiff;

    int i,n,a,b;

    scanf("%d",&n);

    scanf("%d%d",&a,&b);

    memories = a <b? a: b;

    maxdiff=b-a;

    for (i=3;i<=n;i++)

    {

        scanf("%d",&a);

        if (a <min) = a caution;

        if (a memory> maxdiff) = maxdiff a memory;

    }

    printf("%d\n",maxdiff);

    return 0;

}

13-2 successive natural numbers and

Title Description

For a given natural number M, to obtain a natural number of all consecutive segments, the successive natural numbers and the sum of the total number of segments M.

Example: 1998 + 1999 + 2000 + 2001 + 2002 = 10000, so a solution from a natural number 10000 1998-2002 period is M =.

Input Format

It contains an integer value of M given in a single line (10≤M≤2,000,000).

Output Format

Each row two natural numbers, a first given number of successive natural numbers and the last section number of conditions are met, separated by a space between the two numbers, all output lines of a first press in ascending order from small to large , for a given input data, to ensure that there is at least one solution.

SAMPLE INPUT

10000

Sample Output

18 142

297 328

388 412

1998 2002

        (1) programming ideas.

        Define two variables for indicating the left and right sequence to be Qiuzi leftmost and rightmost initial seasonal left = 1, right = (int) sqrt (2.0 * n), clearly this case 1 + 2 + ... + right of and the value of sum (sum = (right-left + 1) * (left + right) / 2) will be very close to n. After the process as follows:

        1) Comparison with the sum n, different processing is performed according to the magnitude relation between sum and n. If the description is simply the sum value is large, to remove subsequences leftmost number, thereby reducing the sum value; if the sum value is small, the next count sequence of the rightmost added, thereby increasing the sum value; if the sum of n it is equal, and to find a sequence equal to n, left and right output values ​​at this time sequence, and outputs the sum value of the sequence number of the leftmost removed rearwardly to continue to find other sequences.

        2) repeat the process, until the left == right, in this case only a sequence number, the search ends, exit.

      (2) source.

#include <stdio.h>

#include <math.h>

int main ()

{

     int left,right,sum,n;

     scanf("%d",&n);

     left=1;   right=(int)sqrt(2.0*n);

     sum=(right-left+1)*(left+right)/2;

     while (left<right)

     {

           if (sum==n)

           {

                    printf("%d %d\n",left,right);

                    sum-=left++;

           }

          else if (sum<n)

           {

                      right++;

                      sum+=right;

           }

           else

           {

                     sum-=left;

                     left++;

           }

     }

    return 0;

}

13-3  Subsequence

The title North POJ selected exam (http://poj.org/problem?id=3061).

Description

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

Input

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

Sample Input

2

10 15

5 1 3 5 10 7 4 9 2 8

5 11

1 2 3 4 5

Sample Output

2

3

        (1) programming ideas.

         The title question is intended that: the input integer N and S, N represents the number of columns of elements, S indicates a sum, and then enter the number of columns in the N elements, seeking a sequence length of the shortest continuous sequence, so that the sub-sequence all elements and greater than or equal to S. The length of the output sequence.

        With the two variables i and j represent two endpoints sequence, the initial values ​​of i and j are 0, and a value sum = 0.

        This search process is simply described as: let move the right end, the right end point and the accumulation value and the value of the sum of (statement sum + = a [j ++]), until the sum is equal to greater than S, the answers are updated as required; then let point moves left, the left end point value and subtracted from the sum value and the (statement sum- = a [i ++];), the answer is also updated until after the sum is less than S, then the right end point moves. Repeat this process until the right point past the last data of the original sequence. 

       (2) source.

#include <stdio.h>

int main ()

{

     int t,i,j,a[100001],n,s,sum,minx;

     scanf("%d",&t);

     while(t--)

     {

        scanf("%d%d",&n,&s);

        for(i=0;i<n;i++)

           scanf("%d",&a[i]);

        i=0;

        minx=0x7fffffff;

        sum=0;

        for (j=0;j<n;)

        {

             while(sum<s && j<n)

                 sum+=a[j++];

             while(sum>=s)

            {

                 if (minx>j-i) minx=j-i;

                 sum-=a[i++];

            }

        }

        if (minx==0x7fffffff)

             minx=0;

        printf("%d\n",minx);

    }

    return 0;

}

Guess you like

Origin www.cnblogs.com/cs-whut/p/11902351.html
Recommended