Solution -「BalticOI 2004」Sequence
Description
Link.
Given is a sequencen \(A\) of \(n\) intergers.
Construct a stricly increasing sequence \(B\) of \(n\) intergers that makes the sum of \(|B_{i}-A_{i}|\) the smallest.
Solution
First, we make \(a_{i}:=a_{i}-i\). Then we just make "strictly increasing" become "unstrictly increasing".
for \(a_{1}\le a_{2}\le\cdots\le a_{n}\):
When \(B\) is the same as \(A\), it gets the minimum answer.
for \(a_{1}\ge a_{2}\ge\cdots\ge a_{n}\):
When for each \(i\), \(B_{i}=A_{\lfloor\frac{n}{2}\rfloor}\), it gets the minimum answer.
Maybe we can divide \(A\) into m parts.
Such like: \([l_{1},r_{1}],\cdots,[l_{m},r_{m}]\)
that satisfies: for each \(i\), sequence \(A[l_{i},r_{i}]\) is unstrictly increasing/decreasing.
So we can get the answer to each interval.
Let's consider how to merge the answers.
When we're merging two intervals, we can just get the new median of the two intervals.
So things above are just bullshit.
Parallel Searching!
FUCK YOU.
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF=1e18;
int n;
LL a[1000010],b[1000010],ans;
void solve(LL l,LL r,int fr,int ba)
{
if(l>=r||fr>ba) return;
LL mid=(l+r)>>1,tmp=0,mn=INF,pos=0;
for(int i=fr;i<=ba;++i) tmp+=abs(a[i]-mid-1);
mn=tmp,pos=fr-1;
for(int i=fr;i<=ba;++i)
{
tmp-=abs(a[i]-mid-1);
tmp+=abs(a[i]-mid);
if(tmp<mn) mn=tmp,pos=i;
}
for(int i=fr;i<=pos;++i) b[i]=mid;
for(int i=pos+1;i<=ba;++i) b[i]=mid+1;
solve(l,mid,fr,pos),solve(mid+1,r,pos+1,ba);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%lld",&a[i]),a[i]-=i;
solve(-INF,INF,1,n);
for(int i=1;i<=n;++i) ans+=abs(a[i]-b[i]);
printf("%lld\n",ans);
for(int i=1;i<=n;++i) printf("%lld ",b[i]+i);
return 0;
}
Solution -「BalticOI 2004」Sequence的更多相关文章
- Solution -「ARC 104E」Random LIS
\(\mathcal{Description}\) Link. 给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...
- Solution -「Gym 102956B」Beautiful Sequence Unraveling
\(\mathcal{Description}\) Link. 求长度为 \(n\),值域为 \([1,m]\) 的整数序列 \(\lang a_n\rang\) 的个数,满足 \(\not\ ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「AGC 003D」「AT 2004」Anticube
\(\mathcal{Description}\) Link. 给定 \(n\) 个数 \(a_i\),要求从中选出最多的数,满足任意两个数之积都不是完全立方数. \(n\le10^5\) ...
- Solution -「ARC 104F」Visibility Sequence
\(\mathcal{Description}\) Link. 给定 \(\{x_n\}\),对于满足 \(h_i\in[1,x_i]\) 的序列 \(\{h_n\}\),定义序列 \(\{p ...
- Solution -「CTS 2019」「洛谷 P5404」氪金手游
\(\mathcal{Description}\) Link. 有 \(n\) 张卡牌,第 \(i\) 张的权值 \(w_i\in\{1,2,3\}\),且取值为 \(k\) 的概率正比于 \ ...
- Solution -「BZOJ 3812」主旋律
\(\mathcal{Description}\) Link. 给定含 \(n\) 个点 \(m\) 条边的简单有向图 \(G=(V,E)\),求 \(H=(V,E'\subseteq E)\ ...
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「简单 DP」zxy 讲课记实
魔法题位面级乱杀. 「JOISC 2020 Day4」治疗计划 因为是不太聪明的 Joker,我就从头开始理思路了.中途也会说一些和 DP 算法本身有关的杂谈,给自己的冗长题解找借口. 首先,治疗方案 ...
- Solution -「基环树」做题记录
写的大多只是思路,比较简单的细节和证明过程就不放了,有需者自取. 基环树简介 简单说一说基环树吧.由名字扩展可得这是一类以环为基础的树(当然显然它不是树. 通常的表现形式是一棵树再加一条非树边,把图画 ...
随机推荐
- 如何在2023年开启React项目
在这里,我想给你一个新的React项目入门的简要概述.我想反思一下优点和缺点,反思一下作为一个开发者所需要的技术水平,反思一下作为一个React开发者,每个启动项目都能为你提供哪些功能.最后,你将了解 ...
- 【技术积累】Python中的Pandas库【二】
如何在 Pandas 中进行文本的匹配和替换操作? 在 Pandas 中,使用 str 属性与正则表达式可以进行文本的匹配和替换操作.下面是一些常用的方法: str.contains():判断字符串中 ...
- STP生成树实验
实验拓扑 实验需求 所有设备都运行STP 改变阻塞端口 实验步骤 1.所有设备都运行STP ,等到收敛完毕,观察状态 [SW1]stp mode stp [SW2]stp mode stp [SW3] ...
- 【技术积累】Vue.js中的基础概念与语法【一】
写在前面 学习Vue之前最好有前端三驾马车的基础[HTML+CSS+JavaScript] 笔者接了一个从头开发的Vue项目,由于公司急着要,没有时间慢慢像在学校里学了,只能边学边做,现在项目雏形已经 ...
- Codeforces Round #879 (Div. 2) A-E
比赛链接 A 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; bool solve() { i ...
- 用写代码的方式画图-试下PlantUML吧
1 序言 所谓一图胜千言,大家平日在工作中编写文档时,往往都需要画各种图来表达中心思想,比如流程图.时序图.UML 图,很多人选择使用 Axure .PrecessOn.Diagrams(darw.i ...
- 获得 markdown 无序列表格式的文件目录树
tree 命令可以获得文件目录结构,但是放在文档中时,我想用 markdown 无序列表的形式,在编辑器内还可以折叠. 完整解决方案:在 ~/.oh-my-zsh/custom 下添加下述自定义函数( ...
- 全网最详细4W字Flink入门笔记(下)
本文已收录至Github,推荐阅读 Java随想录 微信公众号:Java随想录 目录 Flink State状态 CheckPoint & SavePoint CheckPoint原理 Sav ...
- tcpdump 常用命令
最后更新时间 2021-10-05. Linux 的命令太多,tcpdump 是一个非常强大的抓包命令. 有时候想看线上发生的一些问题: nginx 有没有客户端连接过来-- 客户端连接过来的时候 P ...
- Linux 概念:grub2
GRUB 2是许多发行版的缺省efi模式引导加载程序. grub2 与 grub 的区别 官方手册:https://www.gnu.org/software/grub/manual/grub/html ...