Codeforces Round #618 (Div. 2) E. Water Balance

E. Water Balance
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n water tanks in a row, i-th of them contains ai liters of water. The tanks are numbered from 1 to n from 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 a is 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 n integers 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 ai does 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.

Examples
inputCopy
4
7 5 5 7
outputCopy
5.666666667
5.666666667
5.666666667
7.000000000
inputCopy
5
7 8 8 10 12
outputCopy
7.000000000
8.000000000
8.000000000
10.000000000
12.000000000
inputCopy
10
3 9 5 5 1 7 5 3 8 7
outputCopy
3.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
7.500000000
7.500000000

题意:
给你n个数,你可以这样操作:使区间[l,r]的数变成 他们的平均数,求字典序最小的序列。

思路:
假设有两个数x,y,如果x>y 那么取平均数之后z=(x+y)/ len([x,y]) ,这个z肯定小于x且小于y , 如果x<=y ,那么取平均数后 x反而变大 ,所以只要模拟一下把递减序列进行操作就可以了。

代码:

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define ll long long
//#define ll unsigned long long
#define R register int
#define inf 0x3f3f3f3f
#define mod 1000000007
#define eps 1e-6
#define pi acos(-1)
#define mea (memset(a,0,sizeof(a)))
#define myit set<ll>::iterator
#define myits  multiset<ll>::iterator
#define v30 (1<<30)-1
#define all(x) (x).begin(),(x).end()
#define maxs *s.rbegin()
#define fi first
#define se second
using namespace std;
inline ll read(){
   ll s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
void put1(){ puts("YES") ;}
void put2(){ puts("NO") ;}
void put3(){ puts("-1"); }
ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);}
using namespace std;
 
const int manx=2e6+5;
 
double water[manx]; //水
double block[manx]; //区间块的长度
double ans[manx]; //答案
 
int main()
{
    ll n = read();
    for ( int i=1; i<=n ;i++)
        scanf("%lf",&water[i]);
    ll l = 0;
    for ( int i=1 ; i<=n ;i++){
        ans[++l] = water[i];
        block[l] = 1;
        while ( l>1 && ans[l] < ans[l-1]){
            ans[l-1]=( ans[l-1]* block[l-1] + ans[l] * block[l])
            / ( block[l] + block[l-1]);
            block[l-1] += block[l];
            --l;
        }
    }
    for ( int i=1 ; i<=l ;i++)
        for ( int j=1 ; j<= block[i]; j++)
            printf("%.9lf\n",ans[i] );
    return 0;
}
发布了61 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/JiangHxin/article/details/104251493