PTA A1007&A1008

The fourth day

A1007 Maximum Subsequence Sum (25 分)

Title Contents

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj
​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

word

continuous

English / kən'tɪnjʊəs / US / kən'tɪnjʊəs /
ADJ continuous, ongoing;. Continue; the endless

indices

English / 'ɪndɪsiːz / US /' ɪndɪsiz /

index n;. directory (index complex)

Topic analysis

The largest sub-columns and issues in data structure MOOC courses liking grandmother said again, and he wrote, found the time to write a little bit forgotten, then turned the previous code. . . . Can not bear to look ah = - =, review it again Grandma video that he rewrite it again, the solution is said to be dynamic programming, and did not understand.

Specific code

#include&ltstdio.h>
#include&ltstdlib.h>
#define MAXSIZE 10000
int N;
int a[MAXSIZE];
int begin, maxbegin, maxend;
int maxsum = -1, sum;

int main(void)
{
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
        scanf("%d", &a[i]);
    for (int i = 0; i < N; i++)
    {
        sum += a[i];
        if (sum > maxsum)
        {
            maxsum = sum;
            maxbegin = begin;
            maxend = i;
        }
        if (sum < 0)
        {
            begin = i + 1;
            sum = 0;
        }
    }
    if (maxsum == -1)
        printf("%d %d %d", sum, a[0], a[N - 1]);
    else
        printf("%d %d %d", maxsum, a[maxbegin], a[maxend]);
    system("pause");
}

Reference blog

[C / C ++] Maximum Subsequence Sum / maximum sub-columns and issues

A1008 Elevator (20 分)

Title Contents

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

word

request

English / rɪ'kwest / US / rɪ'kwɛst /

. n-request; need
vt claim request.

denote

English / dɪ'nəʊt / US / dɪ'not /
VT., Said instructions

specified

English / spesɪfaɪd / US / spɛsɪfaɪd /

. V specified; detailed description (the Specify past participle)
ADJ predetermined; Detailed description

fulfill

English / ful'fil / US / ful'fil /
. VT fulfill; achieve; meet; the end (equal fulfil)

Topic analysis

Nothing to say, students add and subtract it.

Specific code

#include&ltstdio.h>
#include&ltstdlib.h>

int N;
int last;
int time;

int main(void)
{
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
    {
        int n;
        scanf("%d", &n);
        int temp = n;
        n = n - last;
        if (n > 0)
            time += n * 6;
        else if (n < 0)
            time += (-n) * 4;
        time += 5;
        last = temp;
    }
    printf("%d", time);
    system("pause");
}

Guess you like

Origin www.cnblogs.com/z-y-k/p/11528920.html