性质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. Pandas库学习笔记(3)---Pandas Series

    Pandas Series Pandas Series基本操作 pandas.Series Series结构如下: pandas.Series(data, index, dtype, copy) 构造 ...

  2. Sonar 扫描之SonarScanner介绍

    Sonar扫描之SonarScanner介绍 SonarScanner用于在构建系统没有指定scanner时使用. 项目配置 在你的项目根目录中创建一个名为 sonar-project.propert ...

  3. jpa+querydsl的平替国产easy-query最好用的orm

    jpa+querydsl的平替国产easy-query最好用的orm 一款国产最强java orm,完美支持可控强类型dsl,外加完美支持对象模型筛选拉取的orm,拥有非常智能的include(s)一 ...

  4. 【Oracle】SQL笔记

    与MySQL的区分 https://www.cnblogs.com/bailing80/p/11440927.html 获取当前系统时间 -- 查询当前系统时间,DUAL为一张临时表 SELECT S ...

  5. 【Layui】06 面板 Panel

    文档地址: https://www.layui.com/doc/element/panel.html 演示案例: <div class="layui-collapse"> ...

  6. 【托普斯的力场】—— ARPG游戏《艾尔登法环》中的人物:托普斯

    地址: https://youtube.com/shorts/oSIbOQ_r4fA?si=F-knFwjZ3iPXqPeS https://www.bilibili.com/video/BV1fV4 ...

  7. 华为显卡已经支持pytorch计算框架

    相关链接: https://support.huawei.com/enterprise/zh/doc/EDOC1100079287/a21c08de https://www.zhihu.com/que ...

  8. jax框架的 Pallas 方式的GPU扩展不可用

    说下深度学习框架的GPU扩展功能的部分,也就是使用个人定制化的GPU代码编写方式来为深度学习框架做扩展. 深度学习框架本身就是一种对GPU功能的一种封装和调用,但是由于太high-level,因此就会 ...

  9. ubuntu18.04 环境下如何为 vscode 安装kite

    传说中的Python环境下最好用的智能补全工具   kite =============================================================== 1.   ...

  10. chatgpt的api联网报错问题解决:openai公司的api联网报错解决

    chatgpt是啥,这里不讲,openai是啥这里也不讲.要知道我们不论是通过网页web使用chatgpt还是使用api方式通过客户端使用chatgpt都是需要使用外国IP的, 为啥我们不能访问ope ...