Programmers send oranges

Title description

Every year at the 1024 Programmer Festival, Dark Horse Programmers will hold a large celebration. This year's Programmer's Day is no exception, and every classmate sends oranges.

In the class, there are n students in a row from front to back, and they have learned the results of these students, of which the i-th student's score is ai . The head teacher wants to assess the number of oranges based on the students' test scores in the previous stage. In order to motivate outstanding students, the following requirements must be met when sending oranges:

  • There must be more oranges among the students with good grades in the adjacent class. If the adjacent classmates have the same grades, they must receive the same amount.
  • Each student is assigned at least one orange

Due to the limited budget, the head teacher hopes to issue as few oranges as possible in accordance with the requirements. Excuse me, how many oranges do I need to prepare?

Input format

The first line is a positive integer n not exceeding 10 ^ 6 , indicating the number of students.

In the next line, there are n  non-negative integers ai not exceeding 10 ^ 9 , indicating the score of the i- th classmate.

Output format

Output the answer, that is, how many oranges need to be prepared.

Sample input:

11
3 6 9 4 7 7 2 13 15 15 19

Sample output:

24

 

 Idea: This is a forward and reverse non-decreasing sub-column. Initialize everyone to have an orange, and then traverse the array, the latter one is higher than the previous score, then the orange is one more, and then traverse the array again Find the sub-columns without subtraction, and finally add up the array to sum.

#include <cstdio>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
#include <map>
using namespace std;
const int inf=999999;//0x7fffff
const long long mod=1e9+7;
const double PI=acos(-1);

long long n,m;
long long ans=0;
boolvis [ 105 ];
 long  long dis [ 10000000 ], get [ 10000000 ];
 int main () 
{ 
    cin >> n;
     for ( int i = 1 ; i <= n; i ++ ) { 
        cin >> dis [i];
         get [i] = 1 ;                                   // Initialize the get array, because each student has at least 1 orange 
    }
     for ( int i = 2 ; i <= n; i ++) {                            // scan the array from left to right, equivalent to from left to right to find unabated child columns   
        if(dis [i]> dis [i- 1 ]) {                       // If the score of the next student is greater than the number of oranges of the previous student, there is one more orange than the previous student 
            get [i] = get [i- 1 ] + 1 ; 
        } 
        else  if (dis [i] == dis [i- 1 ]) {                   // The score is equal to the number of oranges should also be equal 
            get [i] = get [i- 1 ]; 
        } 
    } 
    for ( int i = n- 1 ; i > = 1 ; i-) {                      // Then scan the array from right to left again, also find the non-decreasing sub-column from right to left 
        if (dis [i]> dis [i + 1 ]) {
            get [i] = max ( get [i], get [i + 1 ] + 1 );          // Similarly, the number of oranges with high grades is one more than the number of oranges with low grades next to it, but the first sorting result should be considered. The larger of the last result and the previous result 
        }
         else  if (dis [i] == dis [i + 1 ]) {                // The number of equal oranges should also be equal 
            get [i] = get [i + 1 ]; 
        } 
    } 
    for ( int i = 1 ; i <= n; i ++) {                     // accumulate the number of oranges 
        ans + = get [i]; 
    } 
    cout << ans;
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/xusi/p/12716166.html