1437 迈克步

51nod题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437


有n只熊。他们站成一排队伍,从左到右依次1到n编号。第i只熊的高度是ai。

一组熊指的队伍中连续的一个子段。组的大小就是熊的数目。而组的力量就是这一组熊中最小的高度。

迈克想知道对于所有的组大小为x(1 ≤ x ≤ n)的,最大力量是多少。

Input
单组测试数据。
第一行有一个整数n (1 ≤ n ≤ 2×10^5),表示熊的数目。
第二行包含n个整数以空格分开,a1, a2, ..., an (1 ≤ ai ≤ 10^9),表示熊的高度。
Output
在一行中输出n个整数,对于x从1到n,输出组大小为x的最大力量。
Input示例
10
1 2 3 4 5 4 3 2 1 6
Output示例
6 4 4 3 3 2 2 1 1 1
 
这道题就是一个单调队列题,跟51nod的其它单调队列做法差不多,所以直接把以前的题的code拿来修修改改就行了。
 
 index_left 记录从当前数开始向左数,第一个小于它的数的index 
 index_right 记录从当前数开始向右数,第一个小于它的数的index 
A数组记录从左到右,单调递增的一个数列。  A数组创建规则就是每读一个数,将比它大的数都pop掉,然后加入这个数。
代码如下:
		scanf("%d", &tmp);
		while (A[num] > tmp)
		{
			tmpwidth = i - index_left[index[num]] - 1;
			myresult[tmpwidth] = max(myresult[tmpwidth], A[num]);
			--num;
		}

经过处理得到每个数的index_left与index_right。 然后就可以得到以当前数为最小值的一段连续数列
[index_left+1,index_right-1]。
宽度width的result肯定大于宽度width+1的result
所以
	for (i = n-1; i > 0; --i)
	{
		if (myresult[i]<myresult[i+1])
		{
			myresult[i] = myresult[i+1];
		}	
	}
	
代码:
#include <stdio.h>

#define mymaxsize 200001

int myresult[mymaxsize];     //初始值为0
int A[mymaxsize], index_left[mymaxsize], index[mymaxsize]; //, index_right[mymaxsize]
			//index_right 记录右边第一个小于它的数的index  index_left 记录左边第一个小于它的数的index
int n, i, tmpwidth, tmp, num=1;

int main()
{
	scanf("%d", &n);
//	A[0] = 0;
//	index[0] = 0;
	scanf("%d", &A[1]);
	index[1] = 1;
//	index_left[1] = 0;
	for (i = 2; i <= n; ++i)
	{
		scanf("%d", &tmp);
		while (A[num] > tmp)
		{
			tmpwidth = i - index_left[index[num]] - 1;

			if (myresult[tmpwidth] < A[num])
			{
				myresult[tmpwidth] = A[num];
			}                          //	myresult[tmpwidth] = max(myresult[tmpwidth], A[num]);
			--num;	
			                           //			index_right[num--] = i;	
		}
		
		if (A[num] != tmp)
		{
			index_left[i] = index[num];
			index[++num] = i;
			A[num] = tmp;
		}
		else
		{
			index_left[i] = index_left[index[num]];
			index[num] = i;
		}
	}

	for (i = 1; i <= num; ++i)
	{
		tmpwidth = n - index_left[index[i]];
		if (myresult[tmpwidth] < A[i])
		{
			myresult[tmpwidth] = A[i];
		}                              //	myresult[tmpwidth] = max(myresult[tmpwidth], A[i]);
		                              //		index_right[i] = n + 1;
	}
	for (i = n-1; i > 0; --i)
	{
		if (myresult[i]<myresult[i+1])
		{
			myresult[i] = myresult[i+1];
		}	
	}
	
	for (i = 1; i < n;++i)
	{
		printf("%d ", myresult[i]);
	}
	printf("%d", myresult[n]);

	return 0;
}



 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值