C2. Skyscrapers (hard version)--------------------------------------思维(单调栈)

This is a harder version of the problem. In this version n≤500000

The outskirts of the capital are being actively built up in Berland. The company “Kernel Panic” manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought n plots along the highway and is preparing to build n skyscrapers, one skyscraper per plot.

Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.

Formally, let’s number the plots from 1 to n. Then if the skyscraper on the i-th plot has ai floors, it must hold that ai is at most mi (1≤ai≤mi). Also there mustn’t be integers j and k such that j<iai<ak. Plots j and k are not required to be adjacent to i.

The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.

Input
The first line contains a single integer n (1≤n≤500000) — the number of plots.

The second line contains the integers m1,m2,…,mn (1≤mi≤109) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.

Output
Print n integers ai — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible.

If there are multiple answers possible, print any of them.

Examples
inputCopy

5
1 2 3 2 1
outputCopy
1 2 3 2 1
inputCopy
3
10 6 8
outputCopy
10 6 6
Note
In the first example, you can build all skyscrapers with the highest possible height.

In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer [10,6,6] is optimal. Note that the answer of [6,6,8] also satisfies all restrictions, but is not optimal.
题意:
有n个房子,每个房子可以建的最高层数为a[i],但不可以出现相邻左右两边房子的层数都比中间高的情况,要求总共层数要尽可能的多,输出每个房子应该建几层。

解析:
维护峰值最优
找左右边的第一个比自己小的元素,维护前缀和,找最大的峰值。
l[i]:用单调栈维护左边第一个比它小的数
r[i]:用单调栈维护右边第一个比它小的数
suml[i]:左边的前缀和
sumr[i]:右边的前缀和
然后遍历一遍数组,找到最大的峰值在哪


#include<bits/stdc++.h>
using namespace std;
const int N=5e5+1000;
typedef long long ll;
ll l[N],r[N],suml[N],sumr[N];
ll a[N],b[N];
ll n;
stack<ll> q;
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)
	{
		while(q.size()&&a[q.top()]>=a[i]) q.pop();
		if(q.empty()) l[i]=0;
		else l[i]=q.top();
		q.push(i);
	}
	while(q.size()) q.pop();
	for(int i=n;i>=1;i--)
	{
		while(q.size()&&a[q.top()]>=a[i]) q.pop();
		if(q.empty()) r[i]=n+1;
		else r[i]=q.top();
		q.push(i);
	}
	for(int i=1;i<=n;i++)
	{
		if(l[i]) suml[i]=suml[l[i]]+(i-l[i])*a[i];
		else suml[i]=a[i]*i;
	}
	for(int i=n;i>=1;i--)
	{
		if(r[i]) sumr[i]=sumr[r[i]]+(r[i]-i)*a[i];
		else sumr[i]=a[i]*i;
	}
	ll ans=0;
	ll pos=0;
	for(int i=1;i<=n;i++)
	{
		if(!pos||suml[i]+sumr[i]-a[i]>ans)
		{
			ans=suml[i]+sumr[i]-a[i];
			pos=i;
		}
	}
	ll mx=b[pos]=a[pos],ma=mx;
	for(int i=pos+1;i<=n;i++) 
	{
		mx=min(mx,a[i]);
		b[i]=mx;
	}
	for(int i=pos-1;i>=1;i--) 
	{
		ma=min(ma,a[i]);
		b[i]=ma;
	}
	for(int i=1;i<=n;i++) cout<<b[i]<<" ";
	cout<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值