CF1300E Water Balance

题目链接

problem

给出一个长度为n的序列,每次可以选择一个区间\([l,r]\)并将区间\([l,r]\)内的数字全部变为这些数字的平均数。该操作可以进行任意多次。

求出进行任意次操作后可以得到的字典序最小的序列。

solution

可以证明不存在一个数字被进行两次或以上运算。即不存在如下情况:

显然\([2,3]\)处的平均值小于\([1,2]\)处的平均值(否则红色线不会包含2,3)。4处的值大于\([1,3]\)的平均值(否则红色线会包含4)。然后蓝色线进行选择的时候,因为\([1,3]\)内的值全都变成了\([1,3]\)的平均值,而且\([1,3]\)的平均值又比4处的值小,所以蓝色线肯定会包含1号位置。

所以就枚举一下每个区域的右端点,如果前面划分的区域加上当前区域后平均值变小,那么就这这两个区域合并即可。

code

/*
* @Author: wxyww
* @Date: 2020-02-10 09:22:11
* @Last Modified time: 2020-02-10 09:32:02
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<bitset>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 1000010;
ll read() {
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
ll a[N],q[N],p[N],top;
int main() {
    int n = read();
    for(int i = 1;i <= n;++i) a[i] = read();

    for(int i = 1;i <= n;++i) {
        ll sum = a[i],cnt = 1;

        while(top && q[top] * cnt >= sum * p[top]) {
            sum += q[top];cnt += p[top];--top;
        }

        q[++top] = sum;p[top] = cnt;
    }

    for(int i = 1;i <= top;++i) {
        double t = 1.0 * q[i] / p[i];
        for(int j = 1;j <= p[i];++j) {
            printf("%.9lf\n",t);
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/wxyww/p/CF1300E.html

时间: 2024-07-31 05:35:24

CF1300E Water Balance的相关文章

CodeForces 1299C Water Balance

首先,$\mathcal O(n^2)$ 的思路十分好想. 大概就是说,我们发现答案序列是单调不降的,所以倒着来,对于每个位置 $i$,我们找一个它后面的位置 $j$,使得 $\frac{\sum_{k=i}^{j} a_k}{j-i+1}$ 是最小的,然后把这一段全部赋值为这个值.这样,我们就在优先确保前面的值更优时,得到了一个可靠的贪心策略. 因为 $10^6$ 肯定要更好的方法,所以考虑优化.当处理位置 $i$ 的时候,$(i, n]$ 已经处理好了,成为了一个不降的,若干个连续段组成的数

使用async解决nodejs异步问题

项目要求:1.对用户的煤.水.电的使用金额对用户进行每周短信提醒. 2.当爬虫爬来的煤.水.电的剩余金额小于10元时,对用户进行短信提醒. 数据库描诉:mongodb  建了4张表    分别分 每周提醒表.水费表.电费表.煤气表 每周提醒表:用户名.用户id.是否开/关提醒功能.用户是否关联煤.水.电.用户电话 电费表:用户名.用户id.社区编号.供电公司编号.是否开/关提醒.剩余电费.用户电话.更新时间.上次更新时间.上次更新剩余电费. 遇到问题描诉:使用nodejs开发,实现每周六晚上短信

Water Problem

water problem 发布时间: 2015年10月10日 15:34   时间限制: 1000ms   内存限制: 256M 描述 题意很简单 给你N个数, Q个查询 每次查询给你一个区间[L, R] 你要找出 [L, R] 这个区间里面取模M后的最大值. 输入 第一行一个T,表示测试数据组数.第二行两个整数N, M (1<=N<=10^5, 1<=M<=10^9).第三行给你N个整数 整数范围在1到10^9之间.第四行给你一个整数Q. ( 1<=Q<=10^5)

LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级

LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统. 逻辑可分为: 1调度层 (Director):它是整个集群对外面的前端机,负责将客户的请求发送到一组服务器上执行,而客户认为服务是来自一个IP地址(我们可称之为虚拟IP地址)上的. 2[服务器池(server pool)/集群层(Real server)]:是一组真正执行客

轻量级网络流量转发工具 - Balance

Balance是一款具有负载均衡和故障转移功能的TCP代理软件.支持IPV6地址的监听和转发,支持rr轮询和ip hash. 某些功能可以替代iptables,比如讲来自本地的80端口转发到8080上: iptables是这样写的: iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j DNAT --to 192.168.1.1:8080 balance只需要这样: balance 80 localhost:8080

leetcode -eleven:Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

Container With Most Water ——解题笔记

[题目] Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a con

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

益计算Shprotification.v6.8.15.22+Heat.Balance.v6.12.27.36+Cold.Balance.v2.6.14.18

热增益计算Shprotification.v6.8.15.22+Heat.Balance.v6.12.27.36+Cold.Balance.v2.6.14.18 Andrey.Shirshov.Shprotification.v6.8.15.22 Andrey.Shirshov.Heat.Balance.v6.12.27.36 Andrey.Shirshov.Cold.Balance.v2.6.14.18   "现代ASHRAE标准参考数据用于从人.设备.确定热输入的人工照明,半透明的击剑系数(