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".

  1. for \(a_{1}\le a_{2}\le\cdots\le a_{n}\):

    When \(B\) is the same as \(A\), it gets the minimum answer.

  2. 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的更多相关文章

  1. Solution -「ARC 104E」Random LIS

    \(\mathcal{Description}\)   Link.   给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...

  2. Solution -「Gym 102956B」Beautiful Sequence Unraveling

    \(\mathcal{Description}\)   Link.   求长度为 \(n\),值域为 \([1,m]\) 的整数序列 \(\lang a_n\rang\) 的个数,满足 \(\not\ ...

  3. Solution -「CF 623E」Transforming Sequence

    题目 题意简述   link.   有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...

  4. Solution -「AGC 003D」「AT 2004」Anticube

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个数 \(a_i\),要求从中选出最多的数,满足任意两个数之积都不是完全立方数.   \(n\le10^5\) ...

  5. Solution -「ARC 104F」Visibility Sequence

    \(\mathcal{Description}\)   Link.   给定 \(\{x_n\}\),对于满足 \(h_i\in[1,x_i]\) 的序列 \(\{h_n\}\),定义序列 \(\{p ...

  6. Solution -「CTS 2019」「洛谷 P5404」氪金手游

    \(\mathcal{Description}\)   Link.   有 \(n\) 张卡牌,第 \(i\) 张的权值 \(w_i\in\{1,2,3\}\),且取值为 \(k\) 的概率正比于 \ ...

  7. Solution -「BZOJ 3812」主旋律

    \(\mathcal{Description}\)   Link.   给定含 \(n\) 个点 \(m\) 条边的简单有向图 \(G=(V,E)\),求 \(H=(V,E'\subseteq E)\ ...

  8. Solution -「CF 1342E」Placing Rooks

    \(\mathcal{Description}\)   Link.   在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...

  9. Solution -「简单 DP」zxy 讲课记实

    魔法题位面级乱杀. 「JOISC 2020 Day4」治疗计划 因为是不太聪明的 Joker,我就从头开始理思路了.中途也会说一些和 DP 算法本身有关的杂谈,给自己的冗长题解找借口. 首先,治疗方案 ...

  10. Solution -「基环树」做题记录

    写的大多只是思路,比较简单的细节和证明过程就不放了,有需者自取. 基环树简介 简单说一说基环树吧.由名字扩展可得这是一类以环为基础的树(当然显然它不是树. 通常的表现形式是一棵树再加一条非树边,把图画 ...

随机推荐

  1. 文档在线预览(四)将word、txt、ppt、excel、图片转成pdf来实现在线预览

    @ 目录 事前准备 1.需要的maven依赖 2.后面用到的工具类代码: 一.word文件转pdf文件(支持doc.docx) 二.txt文件转pdf文件 三.PPT文件转pdf文件(支持ppt.pp ...

  2. 【技术积累】Mysql中的SQL高级技巧【一】

    什么是多表查询?如何在MySQL中进行多表查询? 多表查询就是在一个查询中涉及到多个表,通过特定的关联方式连接多个表,并根据条件从中查询出所需要的数据. 多表查询是关系型数据库中最为基础的应用之一. ...

  3. mysql_三大范式

    介绍 数据库的三大范式就是数据库的表应该如何设计,应该注意什么. 第一范式 要求每一张表都有一个主键,每一个字段都不可再分. 举例: id username address 1 张三 中国,北京 2 ...

  4. 20200630 excel365 选中一个单元格,对应的行和列都高亮

    Excel默认只高亮选中单元格的行标和列标,在整理数据时容易眼花,如能把这一行和列都高亮岂不是更好.方法在此: 1 打开"开发工具"菜单 默认这一项是隐藏的.文件-选项-自定义功能 ...

  5. Mybatis的ResultMap对column和property的理解

    Mybatis的ResultMap对column和property的理解 首先,先看看这张图,看能不能一下看明白: select元素有很多属性(这里说用的比较多的): id:命名空间唯一标识,可以被用 ...

  6. in用不用索引,啥时候能用啥时候不能用,一文说清

    in/or到底能不能用索引应该是肯定的,但有时生效有时不生效,这个能不能量化计算?这是本文想讨论和解答的问题. in到底用不用索引感觉像一桩悬疑片!古早时期的面经,统一说不走索引,在一些程序员脑海中从 ...

  7. 曲线艺术编程 coding curves 第十四章 其它曲线(Miscellaneous Curves)

    第十四章 其它曲线(Miscellaneous Curves) 原作:Keith Peters https://www.bit-101.com/blog/2022/11/coding-curves/ ...

  8. 在js中修改less文件内样式

    在项目中使用使用进度条时遇到了一点问题,根据需求进度条的百分比需要在条内显示,但是当完成度太低时由于进度条背景和百分比值都是接近的颜色,所以此时无法显示进度值,这个时候需要根据完成度大小来进行判断,动 ...

  9. ObjectInputStream_报错问题

    报错: Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: CE ...

  10. Java扩展Nginx之五:五大handler(系列最核心)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<Java扩展Nginx> ...