性质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. npm私服 verdaccio 搭建

    1.什么是npm 私服 我们前端(web,nodejs)平常使用的各种包,什么vue,react,react-router, zustand等,都会从 https://registry.npmjs.o ...

  2. 分享一个idea的文档汉化插件

    展示效果: 如何获取:

  3. [春秋云镜] Initial

    [春秋云镜] Initial **整套网络环境拓扑:** ​​ 一.打进内网 开局一个ip:39.101.184.25,fscan扫一下 ​​ 存在thinkphp5.0.23的漏洞,可以rce,我们 ...

  4. Jmeter函数助手12-threadNum

    threadNum函数用于获取当前线程编号.该函数没有参数,直接引用即可. 1.线程数可在组件[测试计划->线程组]设置.如下是不传入循环次数的${__threadNum}. "调试取 ...

  5. Jmeter函数助手3-RandomString

    RandomString函数用于生成指定内容范围的指定长度随机字符. Random string length:限制生成的长度,比如输入6则会生成6位字符 Chars to use for rando ...

  6. 13、SpringMVC之异常解析器

    13.1.环境搭建 创建名为spring_mvc_exception的新module,过程参考9.1节和9.5节 13.1.1.创建错误提示页 <!DOCTYPE html> <ht ...

  7. 【Java】Spring注入静态Bean的几种写法

    单例模式在Spring注解上的一种拓展用法 写法一,先配置自身Bean,作为静态成员,然后目标Bean作为自身Bean的实例成员' Spring初始化自身Bean时自动装配数据源Bean,从而附属到静 ...

  8. 【微信小程序】05 设备信息 & 请求API

    获取系统信息(设备信息) https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfo.html ...

  9. 【C3】04 工作原理

    我们已经知道了CSS是做什么的以及怎么写简单的样式这样基础的CSS, 接下来我将了解到浏览器如何获取CSS.HTML和将他们加载成网页. 前置知识: 基础计算机知识.基本软件安装.简单文件知识.HTM ...

  10. douyin 今日头条 巨量登录滑块和douyin详情滑块分析

    声明(lianxi a15018601872) 本文章中所有内容仅供学习交流使用,不用于其他任何目的,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均 ...