题目地址:

https://www.luogu.com.cn/problem/CF13C

题面翻译:
给定一个序列,每次操作可以把某个数加111或者减111。要求把序列变成非降数列。而且要求修改后的数列只能出现修改前的数。

题目描述:
Little Petya likes to play very much. And most of all he likes to play the following game:

He is given a sequence of NNN integer numbers. At each step it is allowed to increase the value of any number by 111 or to decrease it by 111 . The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.

The sequence aaa is called non-decreasing if a1≤a2≤...≤aNa_{1}\le a_{2}\le ...\le a_{N}a1​≤a2​≤...≤aN​ holds, where NNN is the length of the sequence.

输入格式:
The first line of the input contains single integer NNN ( 1≤N≤50001\le N\le 50001≤N≤5000 ) — the length of the initial sequence. The following NNN lines contain one integer each — elements of the sequence. These numbers do not exceed 10910^{9}109 by absolute value.

输出格式:
Output one integer — minimum number of steps required to achieve the goal.

求最小操作次数可以参考https://blog.csdn.net/qq_46105170/article/details/126434434
。这题里要求最后得到的序列只能含修改前的数,所以这里只需要证明存在一个最优解恰好只含修改前的数即可。

设修改前的数集合为AAA,修改后得到的序列为bbb,且操作次数最少,设aia_iai​从小到大排好序之后所得的序列为ai′a'_iai′​。如果bi∉Ab_i\notin Abi​∈/A,假设aj′<bi<aj+1′a'_j<b_i<a'_{j+1}aj′​<bi​<aj+1′​(bib_ibi​必然在某两个a′a'a′之间的,否则只能让操作次数变得更大),进一步假设iii是使得上面不等式成立的最小下标,设aj′<bi≤bi+1≤...≤bi+c<aj+1′<bi+c+1a'_j<b_i\le b_{i+1}\le ...\le b_{i+c}<a'_{j+1}<b_{i+c+1}aj′​<bi​≤bi+1​≤...≤bi+c​<aj+1′​<bi+c+1​。考虑ai,ai+1,...,ai+ca_i,a_{i+1},...,a_{i+c}ai​,ai+1​,...,ai+c​,设这些数大于等于aj+1′a'_{j+1}aj+1′​的数有xxx个,小于等于aj′a'_jaj′​的数有yyy个,如果x>yx>yx>y,那么将bi,bi+1,...,bi+cb_i,b_{i+1},...,b_{i+c}bi​,bi+1​,...,bi+c​整体上移可以得到更优解,矛盾;同理如果x<yx<yx<y也矛盾。当x=yx=yx=y的时候,可以直接将bi,bi+1,...,bi+cb_i,b_{i+1},...,b_{i+c}bi​,bi+1​,...,bi+c​整体下移使得bib_ibi​移动到aj′a'_jaj′​处,仍然是最优解。所以经过有限步就可以使得∀i,bi∈A\forall i, b_i\in A∀i,bi​∈A。

代码如下:

#include <iostream>
#include <queue>
using namespace std;
using LL = long long;const int N = 5010;
int n, a[N];LL solve() {LL res = 0;priority_queue<int> heap;for (int i = 1; i <= n; i++) {heap.push(a[i]);if (a[i] < heap.top()) {res += heap.top() - a[i];heap.pop();heap.push(a[i]);}}return res;
}int main() {scanf("%d", &n);for (int i = 1; i <= n; i++) scanf("%d", &a[i]);printf("%lld\n", solve());
}

时间复杂度O(Nlog⁡N)O(N\log N)O(NlogN),空间O(N)O(N)O(N)。

【CodeForces】CF13C Sequence(配数学证明)相关推荐

  1. 【Leetcode】1526. Minimum Number of Increments on Subarrays to Form a Target Array(配数学证明)

    题目地址: https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array ...

  2. 【洛谷】P1150 Peter的烟(配数学证明)

    题目地址: https://www.luogu.com.cn/problem/P1150 题目描述: Peter有nnn根烟,他每吸完一根烟就把烟蒂保存起来,kkk(k>1k>1k> ...

  3. 【ACWing】2135. 马步距离(配数学证明)

    题目地址: https://www.acwing.com/problem/content/description/2137/ 在国际象棋和中国象棋中,马的移动规则相同,都是走"日" ...

  4. 【洛谷】P2708 硬币翻转(配数学证明)

    题目地址: https://www.luogu.com.cn/problem/P2708 题目描述: 有很多个硬币摆在一行,有正面朝上的,也有背面朝上的.正面朝上的用111表示,背面朝上的用000表示 ...

  5. 【Leetcode】984. String Without AAA or BBB(配数学证明)

    题目地址: https://leetcode.com/problems/string-without-aaa-or-bbb/ 给定两个数xxx和yyy,都是非负整数,要求返回任意一个字符串,其含xxx ...

  6. 陈景润定理的数学证明何处寻

    由于时代过于久远,陈景润定理的数学证明与公式推理过程很难寻找. 实际上,陈景润定理的数学证明与公式推导十分复.困难,出乎一般人的想象. 有兴趣者,可搜索该文PDF原文第,查看第5-6页.该文件共有74 ...

  7. 二叉树相关性质以及数学证明

    性质一: 在二叉树中,设度为0的结点数为n0,度为2的结点数为n2,有n0=n2+1在二叉树中,设度为0的结点数为n_0,度为2的结点数为n_2,有n_0=n_2+1在二叉树中,设度为0的结点数为n0 ...

  8. 较真的来了!这篇【硬核论文】为何恺明新作MAE提供了一种理论解释和数学证明...

    关注公众号,发现CV技术之美 昨天,arXiv上出现了一篇非常硬核的论文"How to Understand Masked Autoencoder".该论文为何恺明的最新一作论文& ...

  9. 【LuoguP4275】萃香的请柬-数学证明

    测试地址:萃香的请柬 做法:本题需要用到找规律(划掉)数学证明. 首先我们要猜(划掉)观察出两个结论: 第一:若第一个字符串为L,第二个字符串为B,则以后任意字符串都是前一个字符串后面接上前一个字符串 ...

最新文章

  1. web标准的商业价值
  2. 如何检查JavaScript中的数字是否为NaN?
  3. DeskArtes 3Data Expert Ultimate中文版
  4. 天融信TOS系统命令行下查看资源使用情况
  5. Linux下读写芯片的I2C寄存器
  6. 使用adduser命令在Debian Linux中创建用户
  7. JavaScript 面向对象编程(一) —— 面向对象基础
  8. Netty粘包拆包问题说明、演示拆包粘包情况代码以及解决
  9. 蔚来三元铁锂电池绕道超车
  10. flutter去掉输入框最大字数显示
  11. java web项目_一个完整JavaWeb项目开发总结
  12. 2016vijos 1-1 兔子的字符串(后缀数组 + 二分 + 哈希)
  13. 调整Poker 在Mac、win下通用键位,解决mac的复制粘贴问题
  14. U盘量产后USB鼠标和键盘都无法使用,如何解决?
  15. RabbitMQ连接超时问题
  16. 两台计算机的ip地址怎么配置,同一台电脑如何设置两个IP地址?电脑配置双ip地址图文教程...
  17. linux中安shell怎么传入参数,【linux】linux 下 shell命令 执行结果赋值给变量【两种方式】...
  18. 远程连接出现 登陆失败:用户账户限制 解决方案
  19. golang与手机如何实现一些自动化操作?
  20. idea 如何连接服务器

热门文章

  1. 鸿蒙系统和小米系统区别,华为鸿蒙系统对比小米MIUI12,到底谁更强!
  2. safari浏览器在使用videojs-contrib-quality-levels.js 播放视频时 清晰度失效, 报错Unhandled Promise Rejection: AbortError
  3. 深入理解JavaScript系列(7):S.O.L.I.D五大原则之开闭原则OCP
  4. 2022年度东湖高新区国家重点研发计划“先进结构与复合材料”重点专项申报指南!
  5. Quagga服务器安装和配置
  6. iOS端URL编码和解码过程
  7. 关于kindle无法联网(连wifi)解决方案
  8. 威纶触摸屏使用U盘/SD卡上传或下载程序步骤及编译失败处理对策
  9. 关于信息安全服务资质认证规范及实施规则换版的通知
  10. 注册dll文件的方法集合