CF547B Mike and Feet

简介: CF547B Mike and Feet

Mike is the president of country What-The-Fatherland. There are n n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 1 to n 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 x such that 1<=x<=n the maximum strength among all groups of size x .

The first line of input contains integer n (1<=n<=2×10 ^5), the number of bears.


The second line contains n n integers separated by space,a1,a2,…,an(1<=ai <=10 ^9), heights of bears.


输出格式:

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


题意:有一个长度为n的序列,序列有长度为1…n的连续子序列,一个连续子序列里面最小的值称作这个子序列的子序列的strength,

要求出每种长度的连续子序列的最大的strength。

思路:可以用单独栈求出每个点的L[i],R[i],以当前点为最小值的左右边界

然后排序一遍(按值排序)时间复杂度为排序复杂度(nlogn)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int L[maxn], R[maxn], a[maxn];
stack<int> s;
struct node {
  int w, len;
}b[maxn];
int ans[maxn];
bool cmp(node a, node b) {
  return a.w > b.w;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  a[0] = 0; a[n + 1] = 0;
  for (int i = 1; i <= n + 1; i++) { //处理右边界
    while (!s.empty() && a[s.top()] > a[i]) {
      R[s.top()] = i - 1;
      s.pop();
    }
    s.push(i);
  }
  while (!s.empty()) {
    s.pop();
  }
  for (int i = n; i >= 0; i--) { //处理左边界
    while (!s.empty() && a[s.top()] > a[i]) {
      L[s.top()] = i + 1;
      s.pop();
    }
    s.push(i);
  }
  for (int i = 1; i <= n; i++) {
    b[i].w = a[i];
    b[i].len = R[i] - L[i] + 1;
  }
  sort (b + 1, b + n + 1, cmp); //按大小排序
  int cnt = 1;
  for (int i = 1; i <= n; i++) {
    int len = b[i].len;
    for (cnt; cnt <= b[i].len; cnt++) { //
      ans[cnt] = b[i].w;
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d ", ans[i]);
  }
  return 0;
}
相关文章
|
7月前
|
Python
Python实现Tom与Jerry
Python实现Tom与Jerry
67 0
|
6月前
|
定位技术
牛和 John
牛和 John
41 0
|
Java 决策智能
ACM刷题之路(十)博弈论 jack & rose
ACM刷题之路(十)博弈论 jack & rose
|
程序员 Windows
|
人工智能
CF547B Mike and Feet(单调栈)
CF547B Mike and Feet(单调栈)
86 0
Jack Ma 你当初UT了没?
Jack Ma的处女作,当初竟然没有检视和UT,NullPointerException害死人的 https://github.com/Wisdom-Projects/rest-client
1357 0
|
XML Java 数据格式
|
Java 网络安全 开发工具
|
JavaScript UED 前端开发