首页 > 其他分享 >题解 CF1406D【Three Sequences】

题解 CF1406D【Three Sequences】

时间:2023-03-04 23:12:46浏览次数:38  
标签:integers sequence 题解 Sequences Three leq every max

看错题了,我很生气。

problem

You are given a sequence of $ n $ integers $ a_1, a_2, \ldots, a_n $ .

You have to construct two sequences of integers $ b $ and $ c $ with length $ n $ that satisfy:

  • for every $ i $ ( $ 1\leq i\leq n $ ) $ b_i+c_i=a_i $
  • $ b $ is non-decreasing, which means that for every $ 1<i\leq n $ , $ b_i\geq b_{i-1} $ must hold
  • $ c $ is non-increasing, which means that for every $ 1<i\leq n $ , $ c_i\leq c_{i-1} $ must hold

You have to minimize $ \max(b_i,c_i) $ . In other words, you have to minimize the maximum number in sequences $ b $ and $ c $ .

Also there will be $ q $ changes, the $ i $ -th change is described by three integers $ l,r,x $ . You should add $ x $ to $ a_l,a_{l+1}, \ldots, a_r $ .

You have to find the minimum possible value of $ \max(b_i,c_i) $ for the initial sequence and for sequence after each change.

\(n,q\leq 10^5\)。

solution

最小化 \(\max(b_1,c_n)\)。

首先考虑:如果 \(b-1\) 同时 \(c+1\),那还不如不变。这也就是说,从 \(i\to i+1\) 调整 \(b,c\) 的时候,一定只有其中一个在变。
具体一点,考虑差分 \(a\):\(d_i=a_i-a_{i-1}\),那么如果 \(d_i>0\) 就给 \(c\),否则给 \(b\)。

设 \(b_1=x\)。则 \(c_1=a_1-x\),\(c_n=c_1+\text{所有正的} d_i\),由于 \(d_i\) 现在固定,我们设 \(K=a_1+\sum_{i}[d_i>0]d_i\)。
则题目是最小化 \(\max(x,K-x)\)。使得 \(x=K-x\) 时原式最小,所以得到了 \(x=K/2\)(有一些上下取整问题,自己枚举一下)。

观察到区间加在差分序列上很容易维护,\(K\) 也很容易维护,于是做完了。

标签:integers,sequence,题解,Sequences,Three,leq,every,max
From: https://www.cnblogs.com/caijianhong/p/solution-CF1406D.html

相关文章

  • LeetCode 29. Divide Two Integers 题解教程 All In One
    LeetCode29.DivideTwoIntegers题解教程AllInOnehttps://leetcode.com/problems/divide-two-integers/description///functiondivide(dividend:number,divis......
  • AGC051E[Middle Point] 题解
    条件转化我们记:\[M=\{x|x=\frac{a}{2^b},a,b\in\mathbb{Z}\}\\M^*=\{x|x\inM,x\ge0\}\]令下文向量均为二维向量,记给定点集为\({\vec{p_n}}\)那么原题即为求满足\(......
  • Java Swing项目使用Idea UI Designer设计插件无法启动问题解决方案
    起因最近整理一下以前写的swing项目,结果发现跑不起来了,具体表现为与视图表绑定的Java类的各属性为NULL(插件没有初始化绑定的类对象),导致项目无法启动。(报空指针异常)问题排......
  • CF1789D Serval and Shift-Shift-Shift 题解
    题目链接题目分析首先,看到题目中的左移右移之后再异或,我们自然可以想到在移动的过程中字符串的一段前缀和后缀不会改变,考虑通过这个性质逐位还原。因为异或0不会改变......
  • 题解 P3455 [POI2007]ZAP-Queries
    题目link是莫比乌斯函数还是莫比乌斯反演捏?感觉好多所谓“莫比乌斯反演”题只要拿\(\mu\)性质给暴力替换一下就能做出来了,比如这题qwq答案是这个式子:\(\sum\limits_{......
  • 萌新也能看懂的 Golang 题解(一)
    写在前面关于“模拟题”和“算法题”及主观难度评价第一批1791.设备编号(模拟)1792.服务器集群网络延时(排序、数学)1793.给定差值的组合(哈希表)1787.最长元音子串(模......
  • 萌新也能看懂的 Golang 题解(二)
    第二批1807.矩阵转置(数学)难度:简单;主观评价:简单。简单模拟题+数学题(判断完全平方数)。先判断矩阵长度是否为完全平方数(开根号然后自身相乘,判断和开根号之前的数是否一致......
  • 萌新也能看懂的 Golang 题解(三)
    第三批1822.电话拦截(模拟、排序)难度:中等;主观评价:简单。sort.Slice() 应用题,重点在于通配符的判断和如何设计数据结构保证最后能按呼叫顺序返回通话记录。对于没有通......
  • 指针和数组笔试题解析
    在大多数情况下,数组名就是数组首元素的地址,但是有两种特殊情况:一、sizeof(数组名):当数组名单独放在sizeof内部,指的是整个数组二、&数组名:取的是整个数组的地址,但是结果和首......
  • CF1764G1 题解
    题意传送门交互库有一个\([1,n]\)的排列\(p\),你可以询问\(l,r,k\),交互库会返回\(\lfloor\dfrac{p_l}{k}\rfloor,\lfloor\dfrac{p_{l+1}}{k}\rfloor,\dots,\lfloor\d......