Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)
题意
n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值
对于x(1<=x<=n) 求出最大的strength值
http://codeforces.com/contest/548/problem/D
思路
我们把每个数作为最小值能最远向左和右用单调栈处理出来,那么可以发现对于x长度的所有group,某个数延伸的区间长度如果大于等于x,则这个数对答案有贡献。
对于样例,我们处理出来后可以观察发现确有此规律:
然后就可以搞啦~
代码
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int g[N],L[N],R[N],a[N],h[N],mx[N];
int main()
{
std::ios::sync_with_stdio(false);
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
a[n+1]=-1;
stack<int> st;
for(int i=1; i<=n; i++)
{
while(!st.empty()&&a[i]<=a[st.top()])
{
st.pop();
}
if(st.empty())
{
L[i]=1;
}
else
{
L[i]=st.top()+1;
}
st.push(i);
}
while(!st.empty()) st.pop();
for(int i=n; i>=1; i--)
{
while(!st.empty()&&a[i]<=a[st.top()])
st.pop();
if(st.empty())
{
R[i]=n;
}
else
R[i]=st.top()-1;
st.push(i);
}
/* for(int i=1; i<=n; i++)
{
cout<<L[i]<<" "<<R[i]<<" "<<endl;
}
cout<<endl;*/
for(int i=1; i<=n; i++)
h[R[i]-L[i]+1]=max(h[R[i]-L[i]+1],a[i]);
for(int i=n;i>=1;i--)
mx[i]=max(mx[i+1],h[i]);
for(int i=1;i<=n;i++)
{
printf("%d ",mx[i]);
}
puts("");
return 0;
}
Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)的更多相关文章
- Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈
B. Mike and Feet Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...
- Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- Codeforces Round #305 (Div. 2) D. Mike and Feet
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #305 (Div. 1) B. Mike and Feet
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax
题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...
- Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力
B. Mike and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...
随机推荐
- remote: http basic: access denied fatal: authentication failed for '‘解决办法
问题描述 由于这个项目代码使用https 进行clone,为什么?因为代码库ssh有问题!fuck! 导致在push代码的时候出现了 remote: http basic: access denied ...
- FLOPS
FLOPS FLOPS(Float Operations Per Second):每秒浮点运算量,是衡量吞吐率的一个单位,通过折算到具体的浮点操作数量上. 所谓,吞吐率——就如同水管,每秒可以流出多少 ...
- echarts使用------地图生成----省市地图的生成及其他相关细节调整
为使用多种业务场景,百度echarts地图示例只有中国地图,那么在使用省市地图的时候,就需要我们使用省市的地图数据了 以下为陕西西安市的地图示例: 此页面引用echarts的js:http://ech ...
- Leetcode173. 二叉搜索树迭代器
空间复杂度O(h)而不是O(n),因此不能直接在初始化函数中做中序遍历将结果存储到数组中.next()和hasNext()时间复杂度为O(1)首先本题很容易想到用二叉树的中序遍历去解决,外加注意点1. ...
- vue 多种方式控制style属性
一共用到了两种方式: 第一种:对象 第二种:数组 看代码: <!doctype html> <html lang="en"> <head> &l ...
- 国内Java面试总是问StringBuffer,StringBuilder区别是啥?档次为什么这么低?
GitHub 6.6k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 6.6k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 6.6k Star 的 ...
- 解惑:在Ubuntu18.04.2的idea上运行Scala支持的spark程序遇到的问题
解惑:在Ubuntu18.04.2的idea上运行Scala支持的spark程序遇到的问题 一.前言 最近在做一点小的实验,用到了Scala,spark这些东西,于是在Linux平台上来完成,结果一个 ...
- java优化细节记录
此处是为了记录一些优化细节,从网上收集而来,仅供后续代码开发参考使用,如发现更好的,会不断完善 首先确认代码优化的目标是: 减小代码的体积 提高代码运行的效率 代码优化细节 1.尽量指定类.方法的fi ...
- N!(hdu1042)
N! Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process ...
- POJ 3041 Asteroids(二分图最大匹配)
###题目链接### 题目大意: 给你 N 和 K ,在一个 N * N 个图上有 K 个 小行星.有一个可以横着切或竖着切的武器,问最少切多少次,所有行星都会被毁灭. 分析: 将 1~n 行数加入左 ...