性质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. XCode 编译 PAG 源码

    最近工作中要使用PAG替换Lottie,为了方便阅读源码,使用XCode对其源码进行了编译. 1 下载源码 编译源码首先要下载源码,有关PAG源码可直接到github上下载. 2 添加相关依赖 下载源 ...

  2. Fusion Compute install

    分区选择默认 配置网络 (使用tab和上下左右 会有红色阴影表示当前选中部分) 密码有复杂度要求 这里输huawei12#$ 一个vrm单节点 两个vrm为主备 FC由vrm与can组成 Vrm提供管 ...

  3. 【Mybatis】11 注解的使用

    文档引用:http://www.mybatis.cn/archives/678.html 视频参考:https://www.bilibili.com/video/BV1NE411Q7Nx?p=15 注 ...

  4. 【SpringMVC】06 转发 & 重定向

    除了快速入门的视图解析器方式处理, 我们还可以使用原生的Servlet转发方式执行 访问测试 还有重定向 访问 测试 使用SpringMVC的转发&重定向 和原生的重定向,有一点不同,MVC的 ...

  5. 【Mybatis-Plus】Spring整合 驼峰命名设置失效问题

    查询时发现这个问题: DEBUG [main] - Creating a new SqlSession DEBUG [main] - SqlSession [org.apache.ibatis.ses ...

  6. 实现一个终端文本编辑器来学习golang语言:第二章Raw模式下的输入输出

    从第二章开始,在每个小节的最后都会有一些代码实操作业,你可以选择自己完成(比较推荐),再对照我的实现方式,当然也可以直接看我的代码实现.不过,之后的各个功能实现,我都会基于我先前的代码实现版本,在它的 ...

  7. OneFlow是否真的实现了单机代码无侵害的运行在分布式集群上?

    答案: 不是,但也是. 严格意义上来说,不是. 因为技术OneFlow的代码,要从单机改到分布式,也需要改配置,需要给所有的变量设置具体的全局存储还是局部存储,如果局部存储又应该如何划分,等等,这些其 ...

  8. Linux系统下使用pytorch多进程读取图片数据时的注意事项——DataLoader的多进程使用注意事项

    原文: PEP 703 – Making the Global Interpreter Lock Optional in CPython 相关内容: The GIL Affects Python Li ...

  9. pytorch之网络参数统计 torchstat & torchsummary

    参考 : https://blog.csdn.net/weixin_45292794/article/details/108227437 https://blog.csdn.net/jzwong/ar ...

  10. 使用SSH连接局域网内的WSL Ubuntu

    参考: https://zhuanlan.zhihu.com/p/586283483 https://www.cnblogs.com/lidabo/p/16855975.html ========== ...