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 -「基环树」做题记录
写的大多只是思路,比较简单的细节和证明过程就不放了,有需者自取. 基环树简介 简单说一说基环树吧.由名字扩展可得这是一类以环为基础的树(当然显然它不是树. 通常的表现形式是一棵树再加一条非树边,把图画 ...
随机推荐
- 图解三代测序(SMRT Sequencing)
目前主流三代测序平台除了Oxford 家的 Nanopore,还有 Pacific Biosciences(简称 PacBio)公司的 Single Molecule Real-Time(SMRT)S ...
- @FunctionalInterface注解的使用
被@FunctionalInterface注解标记的类型表明这是一个函数接口.从概念上讲,函数接口只有一个抽象方法.如果接口声明的抽象方法覆写Object类的公共方法,那这方法不算作接口的抽象方法,因 ...
- 深度Q网络:DQN项目实战CartPole-v0
摘要:相比于Q learning,DQN本质上是为了适应更为复杂的环境,并且经过不断的改良迭代,到了Nature DQN(即Volodymyr Mnih发表的Nature论文)这里才算是基本完善. 本 ...
- CF1794B Not Dividing题解
如果 \(a_i\) 可以整除 \(a_{i - 1}\),只要在 \(a_i\) 上 \(+1\) 即可,这样 \(a_i \bmod a_{i - 1} = 1\) 就满足题目要求了,如果这样算来 ...
- 【2020GET】即构科技蒋宁波:教育行业客户需求的核心是什么?
11月24日,由即构科技主办的2020GET大会教育科技分论坛在北京成功召开,来自叮咚课堂.小冰.360OS.蕃茄田艺术.即构科技的6位资深教育/科技大咖,在论坛上进行深度分享. 以下为即构科技联合创 ...
- PerfView专题 (第十四篇): 洞察那些 C# 代码中的短命线程
一:背景 1. 讲故事 这篇文章源自于分析一些疑难dump的思考而产生的灵感,在dump分析中经常要寻找的一个答案就是如何找到死亡线程的生前都做了一些什么?参考如下输出: 0:001> !t T ...
- DDD架构为什么应该首选六边形架构?
一.传统分层架构 分层架构的一个重要原则是:每层只能与位于其下方的层发生耦合. 分层架构分两种:一种是严格分层架构,规定某层只能与直接位于其下方的层发生耦合:另一种是松散分层架构,允许任意上方层与任意 ...
- expect: telnet2switch
#!/usr/bin/expect if {$argc != 1} { puts "usage: ./telnet2sswitch <r2|r3>" exit } if ...
- zabbix 添加 zabbix_agentd 服务
创建服务文件 # /usr/lib/systemd/system/zabbix-agent.service [Unit] Description=Zabbix Agent After=syslog.t ...
- docker 镜像与容器存储目录结构
目录列表及大小示例-20220314 root@dewan01:/var/lib/docker# du -sh * 88K buildkit 72K containers 884K image 60K ...