性质1:题目操作相当于将前面的数搬到了后面,将其视为柱状图,则是把前面柱的高度转移至后面柱的高度

性质2:最后移成的序列以单调不下降序列为最优,易证明当存在下降时,可通过操作使答案更优或不变差

性质3:由于性质2,易得最佳序列尾部最高,故可以通过栈来维护,其为单调栈,栈口最高

性质4:对于确定的总和sum与某个数(组)的出现次数cnt,由性质2得只有可能有$$\frac{sum}{cnt}$$与$$\frac{sum}{cnt} + 1$$两种取值,易得出现$$\frac{sum}{cnt} + 1$$次数为sum%cnt

性质5:优先用大的数来平衡每次加入且需要平衡的数

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2 * 100010 + 10;
int n;
int T;
int f[N];
struct edge
{
int val;
int cnt;
};
stack <edge> st;
signed main()
{
scanf("%lld",&T);
while(T--)
{
while(!st.empty())st.pop();
scanf("%lld",&n);
edge a;
for(int i = 1;i <= n;i++)
scanf("%lld",&f[i]);
a.val = f[1];
a.cnt = 1;
st.push(a);
for(int i = 2;i <= n;i++)
{
a.val = f[i];
a.cnt = 1;
while( !st.empty() && a.val / a.cnt <= st.top().val / st.top().cnt)
{
edge b = st.top();
st.pop();
a.val += b.val;
a.cnt += b.cnt;
}
edge tt;
tt.val = a.val / a.cnt * (a.cnt - a.val % a.cnt);
tt.cnt = a.cnt - a.val % a.cnt;
st.push(tt);
if(a.val % a.cnt > 0)
{
tt.val = (a.val / a.cnt + 1) * (a.val % a.cnt);
tt.cnt = a.val % a.cnt;
st.push(tt);
}
}
int mx = st.top().val / st.top().cnt;
int mn = -1;
while(!st.empty())
{ mn = st.top().val / st.top().cnt;
st.pop();
} printf("%lld\n",mx - mn);
}
return 0;
}

Codeforces Round 973 (Div. 2) D的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. CF369D Valera and Fools 题解

    题目链接 Luogu Codeforces 题意简述 有 \(n\) 个人站成一排,每人手中有 \(k\) 发子弹,每次每人会向除自己外编号最小的人开枪,第 \(i\) 个人开枪的命中率为 \(p_i ...

  2. 《最新出炉》系列初窥篇-Python+Playwright自动化测试-58 - 文件下载

    1.简介 前边几篇文章讲解完如何上传文件,既然有上传,那么就可能会有下载文件.因此宏哥就接着讲解和分享一下:自动化测试下载文件.可能有的小伙伴或者童鞋们会觉得这不是很简单吗,还用你介绍和讲解啊,不说就 ...

  3. Jmeter函数助手39-isPropDefined

    isPropDefined函数用于判断属性是否存在. 变量的名称:填入属性名.如果属性名存在返回true,如果不存在返回false 1.jmeter的属性查看路径:测试计划右键"添加&quo ...

  4. 【Canal】01 入门 & Kafka模式

    什么是Canal (卡耐尔) ? Canal 是用 Java 开发的基于数据库增量日志解析,提供增量数据订阅&消费的中间件 原理基于MySQL的binlog从库监听       一.MySQL ...

  5. 【微信小程序】04 生命周期 & 事件

    一.应用生命周期: https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.html App(Object object) ...

  6. 【Spring-Security】Re10 Oauth2协议 P1 授权码模式 & 密码模式

    一.Oauth2协议: 第三方登录,即忘记本站密码,但是登录界面中提供了一些第三方登录,例如微信.支付宝.QQ.等等,通过第三方授权实现登录 第三方认证技术主要解决的时认证标准,各个平台的登录要遵循统 ...

  7. 【Hibernate】Re02 官网介绍

    介绍: 创始人:Gavin King.EJB3.0专家,JBoss核心成员之一,<Hibernate In Action>作者 Hibernate是ORM的解决方案. 优点: 1.功能强大 ...

  8. 并行化强化学习 —— 最终版本 —— 并行reinforce算法的尝试

    本文代码地址: https://gitee.com/devilmaycry812839668/final_-version_-parallelism_-reinforce_-cart-pole 结合了 ...

  9. 如何在多台Linux系统主机上实现ssh免密访问——成公钥文件id_rsa.pub(数字签名RSA)

    假设共有三台Linux主机,为matser,slave1,slave2,现在要实现master主机可以ssh免密访问master主机自身以及slave1.slave2. 原理: 主机调用秘钥生成命令, ...

  10. 【转载】 t-SNE是什么? —— 使用指南

    原文地址: https://www.cnblogs.com/LuckBelongsToStrugglingMan/p/14161405.html 转者前言: 该文相当于一个  t-SNE  使用指南, ...