再探water Balance(贪心,单调栈)

题意:
给定一个序列,你每次可以选一段区间,把这个区间的每个数浮赋值成平均值,问最小字典序。
思路:
最后的答案一定是不降的,那么启发我们维护区间的平均值,平均值用区间sum和长度代替,那么就是可以在弹栈的时候可以体现,当后面平均值一直小于前面块的平均值。比较的时候a/b<c/d分别把d和b乘过去,避免精度的影响。
代码:

// Problem: 小x的序列
// Contest: 信息学奥赛比赛系统
// URL: https://qduoj.com/contest/114/problem/C
// Memory Limit: 516 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define IL inline
#define x first
#define y second
typedef long long ll;
using namespace std;
const int N= 1000010;
#define int long long
int l[N];
int r[N];
int w[N];
int a[N];
int top;
int n;
signed main()
{
    cin >> n;
    for (int i= 1; i <= n; i++)
        scanf("%lld", &a[i]);
    l[++top]= 1;
    r[top]= 1;
    w[top]= a[1];
    for (int i= 2; i <= n; i++) {
        int nowl= i;
        int nowr= i;
        int noww= a[i];
        while (top && noww * (r[top] - l[top] + 1) <= (nowr - nowl + 1) * w[top]) {
            nowl= l[top];
            noww+= w[top];
            top--;
        }
        l[++top]= nowl;
        r[top]= nowr;
        w[top]= noww;
    }
    for (int i= 1; i <= top; i++) {
        for (int j= l[i]; j <= r[i]; j++)
            printf("%.4lf\n", 1.0 * w[i] / (r[i] - l[i] + 1));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值