Codeforces548D:Mike and Feet(单调栈)
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 strengthof
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.
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.
Print n integers in one line. For each x from 1 to n,
print the maximum strength among all groups of size x.
10
1 2 3 4 5 4 3 2 1 6
6 4 4 3 3 2 2 1 1 1
题意:
给出n个数,这n个数在区间长度为i(1~n)的时候能够切割成一些区间。这每一个区间都会有一个最小值。在相同长度的这些区间的最小值中,输出最大值
思路:
使用单调栈,保持栈内的数的单调递增性
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 200005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x) /*
num栈顶的元素,width宽度,跟如今入栈的数之间相隔了多少个数,由于要求的是连续区间。所以这个也是必须记录的
*/
struct node
{
int num,width;
node() {};
node(int _num,int _width):num(_num),width(_width) {}
}; stack<node> S;
int a[N],ans[N]; int main()
{
int n,i,j;
scanf("%d",&n);
for(i = 0; i<n; i++)
scanf("%d",&a[i]);
a[n++] = 0;
MEM(ans,0);
for(i = 0; i<n; i++)
{
int len = 0;//连续区间的长度
node k;
while(!S.empty())
{
k = S.top();
if(k.num<a[i])
break;
//新入栈的元素比栈顶元素要小,那么对于这个连续区间而言,这个比新入栈的元素就没实用了,能够出栈
int ls=k.width+len;//出栈的同一时候获得其长度
if(k.num>ans[ls])//ans记录ls区间的时候的最大值
{
ans[ls]=k.num;
}
len+=k.width;
S.pop();
}
S.push(node(a[i],len+1));
}
for(i = n-1; i>=1; i--)//由于上面仅仅更新了一部分的点,所以如今要对那些没有更新的点也更新
ans[i]=max(ans[i],ans[i+1]);
printf("%d",ans[1]);
for(i = 2; i<n; i++)
printf(" %d",ans[i]);
printf("\n"); return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
Codeforces548D: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 547B. Mike and Feet 单调栈
题目链接 用单调栈计算出一个数字, 左边第一个比他小的数字的位置, 右边比第一个他小的数字的位置, 然后len = r[i] - l[i] +1. ans[len] = max(ans[len], a ...
- 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 547B. Mike and Feet[单调栈/队列]
这道题用单调递增的单调栈维护每个数能够覆盖的最大区间即可. 对于 1 2 3 4 5 4 3 2 1 6 这组样例, 1能够覆盖的最大区间是10,2能够覆盖的最大区间是7,以此类推,我们可以使用单 ...
- CF Mike and Feet (求连续区间内长度为i的最小值)单调栈
Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Mike and Feet CodeForces - 548D (单调栈)
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)
题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...
- CodeForces 548D 单调栈
Mike and Feet Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Subm ...
- Mike and Feet(CF 547B)
Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- Android运用自己的标题栏
Android程序的标题栏TitleBar区域很单调,如果想个性化一些可以通过下面的方法来为自己软件的标题定制一个layout布局文件,比如浏览器的标题栏,它包含了网站的Favicon,自定义的进度条 ...
- ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]
门户:problemCode=3635">ZOJ 3635 Cinema in Akiba Time Limit: 3 Seconds Memory Limit: 65536 ...
- Solr在结果返回函数值
如果你有一个服务,你的用户能够搜索不同的公司.用户输入一个简单的keyword,能够返回匹配keyword的公司,可是有一天,用户要求返回的公司列表中能够显示公司跟用户的距离,这是该怎么做呢? 1.如 ...
- UI标签库的话题:JEECG智能开发平台 BaseTag(样式表和JS标签的引入)
UI标签库专题一:JEECG智能开发平台 BaseTag(样式表和JS引入标签) 1.BaseTag(样式表和JS引入标签) 1.1. 演示样例 <t:base type="jquer ...
- client多线程
1.多线程对象 对象可以是多线程访问,线程可以在这里分为两类: 为完成内部业务逻辑的创建Thread对象,线程需要访问对象. 使用对象的线程外部对象. 进一步假设更精细的划分.业主外螺纹成线等线,. ...
- 不用库(框架),自己写ajax
平常会使用ajax来请求数据,加载一个库(框架),或许仅仅maybe就使用了它的ajax部分. 写个ajax,一来可以经历一下处理问题的过程,提升技术能力,二来工作中有时真的用不着这么大的一个库(框架 ...
- SQL Server 2005,2008 正则表达式 替换函数应用详解
CREATE function dbo.regexReplace ( @source ntext, --原字符串 ), --正则表达式 ), --替换值 , --是否是全局替换 --是否忽略大小写 ) ...
- VS2010中配置C#Project不生成.vhost.exe和.pdb文件的方法
在VS2010中编译C#工程时,在C#的Project的属性界面的Build选项卡中当Configuration : Relese 时,依然会生成扩展名为.vhost.exe和.pdb文件. 其中.p ...
- 分散式-ubuntu12.04安装hadoop1.2.1
在hadoop1.2.1被预装在一份报告中安装说明java.我装了很多的版本号java以及许多的版本号hadoop,然后发现oracle-java7与hadoop1.2.1能够匹配. 一,安装详细过程 ...
- Google免费的SVN服务器管理VS2010代码
原文:Google免费的SVN服务器管理VS2010代码 前言 Google免费为我们提供了代码管理的SVN服务器.首先我这里用的Win7 64的电脑系统,用VS2010进行的代码开发.这里管理代码需 ...