[Luogu4331][Baltic2004]数字序列
原题请见《左偏树的特点及其应用》BY 广东省中山市第一中学 黄源河
luogu
题意
给出序列\(a[1...n]\),要求构造一个不下降序列\(b[1...n]\)使得\(\sum_{i=1}^{n}|a_i-b_i|\)最小。
sol
首先很自然地能够想到,构造出来的序列\(b[1...n]\)一定可以划分成\(m\)段\((1\le{m}\le{n})\),每段内数字全部相同。
我们把每一段的数字提取出来分别为\(c[1...m]\)。
如果对每一段的\(c[i]\)都取最优的话,那么一定是去这一段中\(a[i]\)的中位数。
但是取中位数可能会导致序列\(c\)不满足非降,这个时候就需要把相邻的两个不合法的段合并成一段。
所以就需要维护中位数。
左偏树。对于一个长度为\(x\)的段,左偏树中保存这一段中前\(\lfloor\frac{x+1}{2}\rfloor\)小的数字,易知这些数里面最大的那个就是中位数,合并的时候直接合并两棵左偏树。
因为\(\lfloor\frac{x+1}{2}\rfloor+\lfloor\frac{y+1}{2}\rfloor=\lfloor\frac{x+y+1}{2}\rfloor-1\)当且仅当\(x,y\)均为奇数,所以这种情况要弹掉堆顶元素。
复杂度\(O(n\log{n})\)
注:洛谷的题目是要求构造一个递增序列,可以采用减下标的方法,即输入时把每个数都减去对应下表,输出时再加上,这样就可以完成不下降序列和递增序列的转换。
code
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
inline int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 1e6+5;
int n,key[N],ls[N],rs[N],dis[N],S[N],ed[N],top;
ll ans;
int merge(int A,int B)
{
if (!A||!B) return A|B;
if (key[A]<key[B]) swap(A,B);
rs[A]=merge(rs[A],B);
if (dis[ls[A]]<dis[rs[A]]) swap(ls[A],rs[A]);
dis[A]=dis[rs[A]]+1;
return A;
}
int main()
{
n=gi();
for (int i=1;i<=n;++i) key[i]=gi();
for (int i=1;i<=n;++i)
{
++top;S[top]=i;ed[top]=i;
while (top>1&&key[S[top]]<key[S[top-1]])
{
--top;
S[top]=merge(S[top],S[top+1]);
if (((ed[top+1]-ed[top])&1)&&((ed[top]-ed[top-1])&1))
S[top]=merge(ls[S[top]],rs[S[top]]);
ed[top]=ed[top+1];
}
}
for (int i=1;i<=top;++i)
for (int j=ed[i-1]+1;j<=ed[i];++j)
ans+=abs(key[j]-key[S[i]]);
printf("%lld\n",ans);
return 0;
}
强行再贴一个洛谷上那道题的代码
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
inline int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 1e6+5;
int n,key[N],ls[N],rs[N],dis[N],S[N],ed[N],top;
ll ans;
int merge(int A,int B)
{
if (!A||!B) return A|B;
if (key[A]<key[B]) swap(A,B);
rs[A]=merge(rs[A],B);
if (dis[ls[A]]<dis[rs[A]]) swap(ls[A],rs[A]);
dis[A]=dis[rs[A]]+1;
return A;
}
int main()
{
n=gi();
for (int i=1;i<=n;++i) key[i]=gi()-i;
for (int i=1;i<=n;++i)
{
++top;S[top]=i;ed[top]=i;
while (top>1&&key[S[top]]<key[S[top-1]])
{
--top;
S[top]=merge(S[top],S[top+1]);
if (((ed[top+1]-ed[top])&1)&&((ed[top]-ed[top-1])&1))
S[top]=merge(ls[S[top]],rs[S[top]]);
ed[top]=ed[top+1];
}
}
for (int i=1;i<=top;++i)
for (int j=ed[i-1]+1;j<=ed[i];++j)
ans+=abs(key[j]-key[S[i]]);
printf("%lld\n",ans);
for (int i=1;i<=top;++i)
for (int j=ed[i-1]+1;j<=ed[i];++j)
printf("%d ",key[S[i]]+j);
puts("");return 0;
}
[Luogu4331][Baltic2004]数字序列的更多相关文章
- [Baltic2004]数字序列
原题请见<左偏树的特点及其应用>BY 广东省中山市第一中学 黄源河 题意 给出序列\(a[1...n]\),要求构造序列\(b[1...n]\)使得\(\sum_{i=1}^{n}|a_i ...
- 找出数组中最长的连续数字序列(JavaScript实现)
原始题目: 给定一个无序的整数序列, 找最长的连续数字序列. 例如: 给定[100, 4, 200, 1, 3, 2], 最长的连续数字序列是[1, 2, 3, 4]. 小菜给出的解法: functi ...
- 九度OJ 1544 数字序列区间最小值
题目地址:http://ac.jobdu.com/problem.php?pid=1544 题目描述: 给定一个数字序列,查询任意给定区间内数字的最小值. 输入: 输入包含多组测试用例,每组测试用例的 ...
- 【BZOJ】【1049】【HAOI2006】数字序列
DP 第一问比较水……a[i]-=i 以后就变成最长不下降子序列问题了,第二问这个结论好神奇,考试的时候怎么破?大胆猜想,不用证明?TAT 题解:http://pan.baidu.com/share/ ...
- kaggle之数字序列预测
数字序列预测 Github地址 Kaggle地址 # -*- coding: UTF-8 -*- %matplotlib inline import pandas as pd import strin ...
- string 数字序列大小比较
string 数字序列大小比较 string.compare string a = "022"; string b="1"; 比较结果 '022' < ' ...
- codevs 2622 数字序列
2622 数字序列 提交地址:http://codevs.cn/problem/2622/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 De ...
- Shell生成数字序列
转自http://kodango.com/generate-number-sequence-in-shell Shell里怎么输出指定的数字序列: for i in {1..5}; do echo $ ...
- 《剑指offer》第四十四题(数字序列中某一位的数字)
// 面试题44:数字序列中某一位的数字 // 题目:数字以0123456789101112131415…的格式序列化到一个字符序列中.在这 // 个序列中,第5位(从0开始计数)是5,第13位是1, ...
随机推荐
- highchart的用法积累
highcharts 柱子换颜色 var colors = Highcharts.getOptions().colors; $(arr_Y_bfb).each(function (index, ele ...
- js省市二级联动
html: <script src="js/city.js"></script> ...... <body> <div class=&qu ...
- 反编译工具 jad
JAD(Java Decompiler)是一个比较流行的Java反编译工具,可以从网站 http://www.varaneckas.com/jad/ 下载,有多个系统下的应用程序,包括Windows. ...
- js事件总结
事件冒泡: 什么是事件冒泡,就是最深dom节点触发事件,然后逐级向最外层触发事件.打个比方一棵dom tree:li<ul<div每级都有事件绑定,然后我们触发li的事件,这时ul上的事件 ...
- 基于Nginx反向代理及负载均衡
基于Nginx反向代理及负载均衡 参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 只要没有被启用,默认就是 ...
- select2 选择框插件
<select id="selBusi_type"><select> //初始化业务类型下拉 var initBusiTypeSel = function( ...
- 笔记:php有那几种错误提示和查错方法
php有哪几种错误提示 1.notice : 注意 2.waring : 警告 3.error : 错误 PHP中都有哪几种查错方法? 1.语法检查--php配置文件里,把错误显示选项都打开或者代码开 ...
- Java精选笔记_集合【Map(映射)接口】
Map(映射)接口 简介 该集合存储键值对,一对一对的往里存,并且键是唯一的.要保证map集合中键的唯一性. 从Map集合中访问元素时,只要指定了Key,就能找到对应的Value. 关键字是以后用于检 ...
- resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced
Maven在执行中报错: - Failure to transfer org.slf4j:slf4j-api:jar:1.7.24 from http://localhost:8081/nexus/c ...
- List转换为数组Array的方法
List<String> list = new ArrayList<String>(); list.add("str1"); list.add(" ...