• Codeforces Round #305 (Div. 2) D 维护单调栈


    D. Mike and Feet
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.

    A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.

    Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.

    Input

    The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears.

    The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears.

    Output

    Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.

    Examples
    input
    10
    1 2 3 4 5 4 3 2 1 6
    output
    6 4 4 3 3 2 2 1 1 1 

    题意:给你一个长度为n的数列 求长度为x x取值(1,n)  的区段最小值的最大值

    题解:求以a[i]为最小值的区段的左界右界;

    dp[r[i]-l[i]+1]=max(dp[r[i]-l[i]+1],a[i]) 倒序取max得到每个长度的答案;

     1 #pragma comment(linker, "/STACK:102400000,102400000")
     2 #include <cstdio>
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <cmath>
     8 #include <cctype>
     9 #include <map>
    10 #include <set>
    11 #include <queue>
    12 #include <bitset>
    13 #include <string>
    14 #include <complex>
    15 #define ll long long
    16 #define mod 1000000007
    17 using namespace std;
    18 int n;
    19 int a[200005];
    20 int l[200005];
    21 int r[200005];
    22 int ans[200005];
    23 int dp[200005];
    24 int main()
    25 {
    26         scanf("%d",&n);
    27         for(int i=1;i<=n;i++)
    28         scanf("%d",&a[i]);
    29         a[0]=-1;
    30         a[n+1]=-1;
    31         l[1]=1;
    32         for(int i=2; i<=n; i++) //关键********
    33         {
    34             int temp=i-1;
    35             while(a[temp]>=a[i])//维护一个递增的序列
    36                 temp=l[temp]-1;
    37             l[i]=temp+1;
    38         }
    39         r[n]=n;
    40         for (int i=n-1; i>=1; i--)
    41         {
    42             int temp=i+1;
    43             while(a[temp]>=a[i])
    44                 temp=r[temp]+1;
    45             r[i]=temp-1;
    46         }
    47         for(int i=1;i<=n;i++)
    48             dp[r[i]-l[i]+1]=max(dp[r[i]-l[i]+1],a[i]);
    49         int res=0;
    50         for(int i=n;i>=1;i--){
    51             res=max(res,dp[i]);
    52             ans[i]=res;
    53         }
    54         for(int i=1;i<=n;i++)
    55             printf("%d ",ans[i]);
    56         return 0;
    57 }



  • 相关阅读:
    ios开发启动页面
    C++再学习之路(五)
    C++再学习之路(四)
    opencv再学习之路(九)---制作对焦图像
    opencv再学习之路(八)---轮廓检测
    C++再学习之路---例程(一) 文本查询
    opencv再学习之路(五)---灰度直方图显示
    opencv再学习之路(七)---图像单个元素的访问
    opencv再学习之路(六)---模板匹配
    C++再学习之路(三)
  • 原文地址:https://www.cnblogs.com/hsd-/p/7257803.html
Copyright © 2020-2023  润新知