CodeForces 1300 E Water Balance 单调栈+贪心

一、内容

There are n water tanks in a row, i-th of them contains ai liters of water. The tanks are numbered from 1 to nfrom left to right.You can perform the following operation: choose some subsegment [l,r](1≤l≤r≤n), and redistribute water in tanks l,l+1,…,r evenly. In other words, replace each of al,al+1,…,ar by al+al+1+⋯+arr−l+1. For example, if for volumes [1,3,6,7] you choose l=2,r=3, new volumes of water will be [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 ais lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b

Input

The first line contains an integer n(1≤n≤106) — the number of water tanks.The second line contains nintegers a1,a2,…,an (1≤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 i-th line print the final volume of water in the i-th tank.Your answer is considered correct if the absolute or relative error of each aidoes not exceed 10−9.Formally, let your answer be a1,a2,…,an, and the jury's answer be b1,b2,…,bn. Your answer is accepted if and only if |ai−bi|max(1,|bi|)≤10−9 for each i

Input

4
7 5 5 7

Output

5.666666667
5.666666667
5.666666667
7.000000000

二、思路

在这里插入图片描述

三、代码

#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e6 + 5;
double st[N], p[N]; 
int n, len[N];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%lf", &p[i]);
	int t = 0;
	for (int i = 1; i <= n; i++) {
		st[++t] = p[i];
		len[t] = 1;
		//进行合并 有2个以上才用的着合并 
		while (t > 1 && st[t] < st[t - 1]) {
			st[t - 1] = (st[t] * len[t] + st[t - 1] * len[t - 1]) / (len[t - 1] + len[t]);
			len[t - 1] += len[t]; t--;		
		}
	}	
	for (int i = 1; i <= t; i++) {
		for (int j = 1; j <= len[i]; j++) printf("%.9lf\n", st[i]);
	}
	return 0;
}
发布了456 篇原创文章 · 获赞 466 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104457748