[Title] byte brush beating second question written test algorithm 2018

时间限制:3秒 空间限制:131072K

Given an array sequence, selecting a request interval, such that the interval is the interval after all calculated as a maximum value of:

Interval minimum number * number of all sections and the last program outputs the maximum value to the calculation, the output does not need specific interval. The given sequence [621] According to the above formula, all the calculated value is obtained for each section can be selected:

[6] = 6 * 6 = 36;

[2] = 2 * 2 = 4;

[1] = 1 * 1 = 1;

[6,2] = 2 * 8 = 16;

[2,1] = 1 * 3 = 3;

[6, 2, 1] = 1 * 9 = 9;

From the above calculation of the selected interval [6], the calculated value 36, the program 36 is output.

All numbers within the range are in the range [0, 100];

Input Description:
The first line of the input array sequence length n, a second row input array sequence.
For 50% of the data, 1 <= n <= 10000 ;
to 100% of the data, 1 <= n <= 500000 ;

Output Description:
maximum value after calculation of the output array.

Examples 1 Input:
. 3
. 6 1 2

Output Example 1:
36

Ideas:
Each element is likely to be a minimum of one or more intervals, so when the determined minimum range the bigger the better, the minimum can be fixed to each, and then find the interval left and right borders,

#include<iostream>
#include<vector>
#include<limits.h>
#include<algorithm>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int> nums(n);
    for(int i=0; i<n; i++) cin>>nums[i];
    // 每次假设当前为最小
    int cur_max = -1;
    for(int i=0; i<n; i++){
        // 假设nums[i]是某个区间的最小值,那么这个区间应该越大越好,所以可以来找区间的左边界和右边界
        int l=i;
        while(l>=0 && nums[l]>=nums[i]) l--;
        int r = i;
        while(r<n && nums[r]>=nums[i]) r++;
        int sum = accumulate(nums.begin()+l+1, nums.begin()+r, 0);
        cur_max = max(cur_max, sum*nums[i]);
    }
    cout<<cur_max<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/Elaine-DWL/p/11210621.html