传送门
题目描述
给一个序列,每次可以将一个区间内的所有数都变成操作前这个区间的平均数,求最后能得到的字典序最小的结果。
分析
结论:最后的的序列一定是一个不下降的序列,因为如果出现一个下降序列,就可以进行合并,将前面的数字转换成相对较小的数,字典序较小
所以我们可以将每一位数字当作一个区间,然后维护一个栈,如果发现栈顶元素的平均数大于将要将要入栈的元素,就弹栈,然后合并,最后维护一下区间就好了
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e6+ 10;
double a[N];
struct Node{
int l,r;
double sum;
};
stack<Node> Q;
int n;
int main(){
scanf("%d",&n);
for(int i = 1;i <= n;i++) scanf("%lf",&a[i]);
Q.push({1,1,a[1]});
for(int i = 2;i <= n;i++){
Node back = {i,i,a[i]};
while(Q.size()){
double res = back.sum / (back.r - back.l + 1);
auto t = Q.top();
Q.pop();
double x = t.sum / (t.r - t.l + 1);
if(res <= x){
back.sum += t.sum;
back.l = min(back.l,t.l);
back.r = max(back.r,t.r);
// cout << back.l << ' ' << back.r << endl;
}
else {
Q.push(t);
break;
}
}
Q.push(back);
}
while(Q.size()){
auto t = Q.top();
Q.pop();
// cout << t.l << ' ' << t.r << endl;
for(int i = t.l;i <= t.r;i++) a[i] = t.sum / (t.r - t.l + 1);
}
for(int i = 1;i <= n;i++)
printf("%.9lf\n",a[i]);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/