Solution -「洛谷 P4372」Out of Sorts P
\(\mathcal{Description}\)
设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排序,则重复冒泡排序零次或多次,直到存在某个位置 \(p\in[l,r)\),满足 \(\max_{i=l}^p\{a_i\}<\min_{i=p+1}^r\{a_i\}\),则递归入 \([l,p]\) 和 \((p,r]\),直到区间长度为 \(1\) 时停止。求所有冒泡排序所操作的区间长度之和。
\(n\le10^5\),保证 \(\{a_n\}\) 无重复数字。
\(\mathcal{Solution}\)
可以发现“递归入两个区间”是对冒泡排序一个并没有什么用的剪枝——两个区间之间一定不会出现元素交换。那么这个剪枝完全可以忽略,算法等价于不停对 \([1,n]\) 冒泡直到序列有序,唯一的区别仅有代价不同。但好处在于,以忽略递归的排序算法为基础,容易求出每个位置什么时候成为分割点 \(p\)——即不停对 \([1,n]\) 冒泡,什么时候 \(\forall a_j\in[1,i],~j\in[1,i]\):这个值就是离 \(i\) 最远的满足 \(a_j\in[1,i]\) 的 \(j\) 到 \(i\) 的距离,倒着扫一遍 BIT 维护即可。
求出每个位置成为分隔点的最早时间 \(t_i\),接下来的做法包括但不限于:
- 按题意模拟!启发式分裂模拟排序算法,\(\mathcal O(n\log n)\);
- 单调栈!扫一遍就行,\(\mathcal O(n)\);
- 算每个点的贡献!排序 \(\max\{t_{i-1},t_{i+1}\}\) 次之后,\(i\) 才会变成长度为 \(1\) 的区间,这就是对答案的贡献,\(\mathcal O(n)\)。
\(\mathcal{Code}\)
按题意模拟好啊,脑细胞多精贵啊。(
注意洛谷原题中,算法会先冒泡一次再检查分割点,细节需要改改。
/*~Rainybunny~*/
#include <bits/stdc++.h>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
inline char fgc() {
static char buf[1 << 17], *p = buf, *q = buf;
return p == q && ( q = buf + fread( p = buf, 1, 1 << 17, stdin ), p == q )
? EOF : *p++;
}
template<typename Tp = int>
inline Tp rint() {
Tp x = 0; int f = 1; char s = fgc();
for ( ; s < '0' || '9' < s; s = fgc() ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = fgc() ) x = x * 10 + ( s ^ '0' );
return x * f;
}
template<typename Tp>
inline void wint( Tp x ) {
if ( x < 0 ) putchar( '-' ), x = -x;
if ( 9 < x ) wint( x / 10 );
putchar( x % 10 ^ '0' );
}
inline void chkmax( int& a, const int b ) { a < b && ( a = b ); }
inline int imin( const int a, const int b ) { return a < b ? a : b; }
const int MAXN = 1e5, MAXLG = 16;
int n, a[MAXN + 5], dc[MAXN + 5], st[MAXN + 5][MAXLG + 5], bitw[MAXN + 5];
long long ans;
struct BIT {
int val[MAXN + 5];
inline void upd( int x, const int v ) {
for ( ; x <= n; x += x & -x ) chkmax( val[x], v );
}
inline int ask( int x ) {
int ret = 0;
for ( ; x; x -= x & -x ) chkmax( ret, val[x] );
return ret;
}
} bit;
inline int qmin( const int l, const int r ) {
int k = bitw[r - l + 1];
return imin( st[l][k], st[r - ( 1 << k ) + 1][k] );
}
inline void solve( const int l, const int r, const int las ) {
if ( l == r ) return ;
int firc = qmin( l, r - 1 ), p = 0;
for ( int len = 0; /* solution always exists */; ++len ) {
if ( st[l + len][0] == firc ) { p = l + len; break; }
if ( st[r - 1 - len][0] == firc ) { p = r - len - 1; break; }
}
ans += ( r - l + 1ll ) * ( firc - las );
solve( l, p, firc ), solve( p + 1, r, firc );
}
int main() {
freopen( "sort.in", "r", stdin );
freopen( "sort.out", "w", stdout );
n = rint();
rep ( i, 1, n ) a[i] = dc[i] = rint();
std::sort( dc + 1, dc + n + 1 ); // no need to unique.
// assert( std::unique( dc + 1, dc + n + 1 ) - dc - 1 == n );
rep ( i, 1, n ) a[i] = std::lower_bound( dc + 1, dc + n + 1, a[i] ) - dc;
bit.upd( a[n], n );
per ( i, n - 1, 1 ) {
if ( ( st[i][0] = bit.ask( i ) ) ) st[i][0] -= i;
bit.upd( a[i], i );
}
rep ( i, 2, n ) bitw[i] = bitw[i >> 1] + 1;
for ( int j = 1; 1 << j <= n; ++j ) {
rep ( i, 1, n - ( 1 << j ) + 1 ) {
st[i][j] = imin( st[i][j - 1], st[i + ( 1 << j >> 1 )][j - 1] );
}
}
solve( 1, n, 0 );
wint( ans ), putchar( '\n' );
return 0;
}
Solution -「洛谷 P4372」Out of Sorts P的更多相关文章
- Note/Solution -「洛谷 P5158」「模板」多项式快速插值
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \((x_i,y_i)\),求一个不超过 \(n-1\) 次的多项式 \(f(x)\),使得 \(f(x ...
- Solution -「洛谷 P4198」楼房重建
\(\mathcal{Description}\) Link. 给定点集 \(\{P_n\}\),\(P_i=(i,h_i)\),\(m\) 次修改,每次修改某个 \(h_i\),在每次修改后 ...
- Solution -「洛谷 P6577」「模板」二分图最大权完美匹配
\(\mathcal{Description}\) Link. 给定二分图 \(G=(V=X\cup Y,E)\),\(|X|=|Y|=n\),边 \((u,v)\in E\) 有权 \(w( ...
- Solution -「洛谷 P6021」洪水
\(\mathcal{Description}\) Link. 给定一棵 \(n\) 个点的带点权树,删除 \(u\) 点的代价是该点点权 \(a_u\).\(m\) 次操作: 修改单点点权. ...
- Solution -「洛谷 P4719」「模板」"动态 DP" & 动态树分治
\(\mathcal{Description}\) Link. 给定一棵 \(n\) 个结点的带权树,\(m\) 次单点点权修改,求出每次修改后的带权最大独立集. \(n,m\le10^5 ...
- Solution -「洛谷 P5236」「模板」静态仙人掌
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的仙人掌,\(q\) 组询问两点最短路. \(n,q\le10^4\),\(m\ ...
- Solution -「洛谷 P4320」道路相遇
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的连通无向图,并给出 \(q\) 个点对 \((u,v)\),询问 \(u\) 到 ...
- Solution -「洛谷 P5827」边双连通图计数
\(\mathcal{Description}\) link. 求包含 \(n\) 个点的边双连通图的个数. \(n\le10^5\). \(\mathcal{Solution}\) ...
- Solution -「洛谷 P5827」点双连通图计数
\(\mathcal{Description}\) link. 求有 \(n\) 个结点的点双连通图的个数,对 \(998244353\) 取模. \(n\le10^5\). \(\mat ...
随机推荐
- 华为云 Kubernetes 管理员实训 三 课后作业
Exercise 1 通过Deployment方式,使用redis镜像创建一个pod.通过kubectl获得redis启动日志. Deployment的名称为<hwcka-003-1-你的华为云 ...
- Antd使用timePicker封装时间范围选择器(React hook版)
antd中提供了是日期范围选择器及datepaicker封装日期范围选择器的示例,但是没有时间选择范围的组件,这里使用两个timePicker组合一个事件范围选择器,通过disabled属性限定时间可 ...
- hadoop入门到实战(6)hive常用优化方法总结
问题导读:1.如何理解列裁剪和分区裁剪?2.sort by代替order by优势在哪里?3.如何调整group by配置?4.如何优化SQL处理join数据倾斜?Hive作为大数据领域常用的数据仓库 ...
- Sentry 开发者贡献指南 - Django Rest Framework(Serializers)
Serializer 用于获取复杂的 python 模型并将它们转换为 json.序列化程序还可用于在验证传入数据后将 json 反序列化回 Python 模型. 在 Sentry,我们有两种不同类型 ...
- 【PTA】6-2 读文章(*) (31 分)
请编写函数,从文件中读出文章,将其输出到屏幕上. 函数原型 void ReadArticle(FILE *f); 说明:参数 f 为文件指针.函数读出 f 所指示文件中的文章,将其输出到屏幕上. 裁判 ...
- Typora图床
Typora图床 Typora+PicGo+Gitee(码云)实现高效Markdown图床 typora是我最早接触的markdown格式的轻文本编辑器,因为我是计算机专业,所以平常记笔记会有代码块, ...
- vivo推送平台架构演进
本文根据Li Qingxin老师在"2021 vivo开发者大会"现场演讲内容整理而成.公众号回复[2021VDC]获取互联网技术分会场议题相关资料. 一.vivo推送平台介绍 1 ...
- leetcode 986. 区间列表的交集
问题描述 给定两个由一些 闭区间 组成的列表,每个区间列表都是成对不相交的,并且已经排序. 返回这两个区间列表的交集. (形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合, ...
- TextBox,RichTextBox设置行高
/// <summary> /// 设置行距 /// </summary> /// <param name="ctl">控件</param ...
- 【源码阅读】VictoriaMetrics中理解vm-backup中设置origin地址的用途
lib/backup/actions/backup.go: // 118 行 partsToCopy := common.PartsDifference(srcParts, dstParts) //要 ...