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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章