题目
题意: 给定长度为n的数组,可以执行任意次operation,最后输出字典序最小的数组。
operation: 选定数组任意子区间,让每个数变成该区间的平均值。
思路: 一种朴素的想法是迭代,如果一个序列是非递减的,取平均不会更优。所以每遇到严格递减的序列,就取平均。之后一直迭代。但是这种做法有点暴力,会被卡掉。
更优秀的做法是二分或者单调栈。
单调栈始终维护一个上升序列,当新遇到的元素小于栈顶时,就一直弹栈,并将两块数合并变成平均值,直到变为单调不减的序列。
时间复杂度: O(n)
代码:
// Problem: C. Water Balance
// Contest: Codeforces - Codeforces Round #618 (Div. 1)
// URL: https://codeforces.com/problemset/problem/1299/C
// Memory Limit: 256 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 1e6+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();T f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
int a[N];
int cnt[N];
double st[N];
int top = 0;
inline void update()
{
while(top > 1)
{
if(st[top] < st[top-1])
{
double tmp = (st[top-1]*cnt[top-1]+st[top]*cnt[top])/(cnt[top]+cnt[top-1]);
st[top-1] = tmp;
cnt[top-1] += cnt[top];
--top;
}
else
{
break;
}
}
}
void solve()
{
read(n);
for(int i=1;i<=n;++i) read(a[i])
for(int i=1;i<=n;++i)
{
st[++top] = a[i];
cnt[top] = 1;
update();
}
for(int i=1;i<=top;++i)
{
for(int j=0;j<cnt[i];++j)
printf("%.9lf\n",st[i]);
}
}
signed main(void)
{
T = 1;
// OldTomato; cin>>T;
// read(T);
while(T--)
{
solve();
}
return 0;
}