CF1299C Water Balance(单调栈)

显然最终的答案是一个不降序列,因为一旦有下降,一定可以将两段合并,这样字典序更小

那么其实就是不断维护加入的过程,刚开始是一个数字一段,一旦前面的平均值大于等于准备插入的,那么就一定要合并,因为这样才能使最后的答案为不降序列

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
typedef pair<int,pll> plll;
const int N=1e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
struct node{
    double sum;
    int l,r;
}s[N];
int a[N];
int main(){
    //ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int i;
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    vector<node> num;
    num.clear();
    num.push_back({(double)a[1],1,1});
    for(i=2;i<=n;i++){
        num.push_back({(double)a[i],i,i});
        while((int)num.size()>1){
            int len=(int)num.size();
            auto x=num[len-2];
            auto y=num[len-1];
            if(x.sum/(x.r-x.l+1)>=(y.sum)/(y.r-y.l+1)){
                num.pop_back();
                num.pop_back();
                num.push_back({y.sum+x.sum,x.l,y.r});
            }
            else{
                break;
            }
        }
    }
    for(auto x:num){
        int i;
        for(i=x.l;i<=x.r;i++){
            printf("%.9f
",x.sum/(x.r-x.l+1));
        }
    }
    return 0;
}
View Code
没有人不辛苦,只有人不喊疼
原文地址:https://www.cnblogs.com/ctyakwf/p/13562282.html