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;
}
相关文章
|
存储 关系型数据库 MySQL
OceanBase数据库 与 mysql 对比
OceanBase数据库 与 mysql 对比
6799 1
|
C++ Windows
[re入门]PE文件结构(上)
[re入门]PE文件结构
420 0
[re入门]PE文件结构(上)
|
关系型数据库 MySQL
maxwell监听MySQL的Binlog
maxwell监听MySQL的Binlog
322 0
|
人工智能 云计算
保持高增速 阿里云连续7个季度规模翻番
本文讲的是保持高增速 阿里云连续7个季度规模翻番【IT168 资讯】2016年1月24日,阿里巴巴公布了截止至 2016 年12月31日的2017 财年第三季度财报。财报显示,云计算依旧是阿里巴巴增速最猛的业务。
1664 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8067 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18469 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全