Water Balance CodeForces - 1300E

There are nn water tanks in a row, ii-th of them contains aiai liters of water. The tanks are numbered from 11 to nn from left to right.

You can perform the following operation: choose some subsegment [l,r][l,r] (1≤l≤r≤n1≤l≤r≤n), and redistribute water in tanks l,l+1,…,rl,l+1,…,r evenly. In other words, replace each of al,al+1,…,aral,al+1,…,ar by al+al+1+⋯+arr−l+1al+al+1+⋯+arr−l+1. For example, if for volumes [1,3,6,7][1,3,6,7] you choose l=2,r=3l=2,r=3, new volumes of water will be [1,4.5,4.5,7][1,4.5,4.5,7]. You can perform this operation any number of times.

What is the lexicographically smallest sequence of volumes of water that you can achieve?

As a reminder:

A sequence aa is lexicographically smaller than a sequence bb of the same length if and only if the following holds: in the first (leftmost) position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.

Input
The first line contains an integer nn (1≤n≤1061≤n≤106) — the number of water tanks.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — initial volumes of water in the water tanks, in liters.

Because of large input, reading input as doubles is not recommended.

Output
Print the lexicographically smallest sequence you can get. In the ii-th line print the final volume of water in the ii-th tank.

Your answer is considered correct if the absolute or relative error of each aiai does not exceed 10−910−9.

Formally, let your answer be a1,a2,…,ana1,a2,…,an, and the jury’s answer be b1,b2,…,bnb1,b2,…,bn. Your answer is accepted if and only if |ai−bi|max(1,|bi|)≤10−9|ai−bi|max(1,|bi|)≤10−9 for each ii.

Examples
Input
4
7 5 5 7
Output
5.666666667
5.666666667
5.666666667
7.000000000
Input
5
7 8 8 10 12
Output
7.000000000
8.000000000
8.000000000
10.000000000
12.000000000
Input
10
3 9 5 5 1 7 5 3 8 7
Output
3.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
7.500000000
7.500000000
Note
In the first sample, you can get the sequence by applying the operation for subsegment [1,3][1,3].

In the second sample, you can’t get any lexicographically smaller sequence.
思路:一开始我们将每一个数字当做一个区域,从后往前遍历,如果当前区域加上之前的区域可以使得平均值变小的话,那么我们就合并这两个区域,这样出来就是最优的了。
代码如下:

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

const int maxx=1e6+100;
struct node{
	ll num;
	ll cnt;
}p[maxx];
ll a[maxx];
int n;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	int fro=0;
	p[++fro].num=a[n];
	p[fro].cnt=1ll;
	for(int i=n-1;i>=1;i--)
	{
		ll cnt=1ll;ll sum=a[i];
		while(fro&&p[fro].num*(p[fro].cnt+cnt)<p[fro].cnt*(p[fro].num+sum))
		{
			cnt+=p[fro].cnt;
			sum+=p[fro].num;
			--fro;
		}
		p[++fro].cnt=cnt;
		p[fro].num=sum;
	}
	for(int i=fro;i>=1;i--)
	{
		for(int j=1;j<=p[i].cnt;j++) printf("%.9lf\n",(double)p[i].num/(double)p[i].cnt);
	}
	return 0;
}

努力加油a啊,(o)/~

发布了426 篇原创文章 · 获赞 24 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104264851