codeforces1299C Water Balance

https://codeforces.com/problemset/problem/1299/C

BCDE都想得太慢了。。。最后没冲完。。。。好菜啊

这题我们可以想到整个序列一定是单调不下降的,如果下降了,要保证字典序最小,那右边的小的跟左边的大的合并一定能使答案最优。

那么搞个单调栈就行了,从左到右,单调栈中维护区间段的长度,和这一区间段的值,单调栈按值从小到大,如果当前进来的比s[top]小,那么合并就行了。

#include<bits/stdc++.h>
using namespace std;

const int maxl=1e6+10;
const double eps=1e-11;

int n,m,top;
int a[maxl];
double sum[maxl],s[maxl];
long long len[maxl];
double ans[maxl];

inline void prework()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{	
		scanf("%d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
}

inline void mainwork()
{
	top=1;s[1]=a[1];len[1]=1;
	int l,nowind,nowlen;
	double now;
	for(int i=2;i<=n;i++)
	{
		now=a[i];nowlen=1;
		while(s[top]>now+eps)
		{
			now=(now*nowlen+s[top]*len[top])/(nowlen+len[top]);
			nowlen+=len[top];
			top--;
		}
		s[++top]=now;len[top]=nowlen;
	}
	int cnt=0;
	for(int i=1;i<=top;i++)
	{
		for(int j=1;j<=len[i];j++)
			ans[cnt+j]=s[i];
		cnt+=len[i]; 
	} 
}

inline void print()
{
	for(int i=1;i<=n;i++)
		printf("%.10f\n",ans[i]);
}

int main()
{
	int t=1;
	//scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值