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 -「基环树」做题记录
写的大多只是思路,比较简单的细节和证明过程就不放了,有需者自取. 基环树简介 简单说一说基环树吧.由名字扩展可得这是一类以环为基础的树(当然显然它不是树. 通常的表现形式是一棵树再加一条非树边,把图画 ...
随机推荐
- CF1583H Omkar and Tours 题解
题意: 给定一个 \(n\) 个点的树,每条边有权值 \(t\) 和 \(c\).一条路径的权值为所经过节点的 \(\max(c)\). 每个点有权值 \(e\). 给出 \(q\) 个询问,每次询问 ...
- Vue——动态组件、插槽、Vue-cli创建项目、Vue项目目录结构、编写规范、es6导入导出语法
动态组件 // <component :is="who"></component> // component标签的is属性等于组件名字,这里就会显示这个组件 ...
- 8. RESTful案例
1. 准备工作 和传统 CRUD 一样,实现对员工信息的增删改查. 搭建环境 准备实体类 package com.atguigu.mvc.bean; public class Employee { ...
- Python编程和机器学习中的自然语言处理:如何从文本中提取有意义的信息和数据
目录 引言 自然语言处理(Natural Language Processing,NLP)是一种人工智能技术,旨在使计算机理解和处理自然语言文本,从中提取有意义的信息和数据.NLP是机器学习领域中的重 ...
- 2023-06-26:在大小为 n x n 的网格 grid 上,每个单元格都有一盏灯,最初灯都处于 关闭 状态 给你一个由灯的位置组成的二维数组 lamps 其中 lamps[i] = [rowi,
2023-06-26:在大小为 n x n 的网格 grid 上,每个单元格都有一盏灯,最初灯都处于 关闭 状态 给你一个由灯的位置组成的二维数组 lamps 其中 lamps[i] = [rowi, ...
- JavaCV人脸识别三部曲之二:训练
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<JavaCV人脸识别三部曲&g ...
- 为什么从 MVC 到 DDD,架构的本质是什么?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 本文来自于小傅哥新编写的<Java简明教程>系列内容,本教程意在于通过简单.明了. ...
- 使用Stable Diffusion生成艺术二维码
在数字艺术的世界中,二维码已经从单纯的信息承载工具转变为可以展示艺术表达的媒介.这是通过使用Stable Diffusion的技术实现的,它可以将任何二维码转化为独特的艺术作品.接下来,我们将一步步教 ...
- 开源BaaS平台Supabase介绍
Supabase 介绍 Supabase 是一个开源的 Firebase 替代品,以BaaS的形式向各种应用程序提供了一系列的后端功能,可以帮助开发者更快地构建产品. 对于想快速实现一个产品而言,如果 ...
- C# HttpClient请求gzip
//设置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecomp ...