JZOJ5947. 【NOIP2018模拟11.02】初音未来(miku)

本文介绍了一种针对音调序列的排序算法,通过维护逆序对的位置并使用set数据结构,实现了对特定区间进行升序排序的操作。适用于音乐排序场景,复杂度为O((n^2+m)logn)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

H e r c i e r Hercier Hercier作为一位喜爱 H a t s u n e M i k u Hatsune Miku HatsuneMiku O I e r OIer OIer,痛下决心,将 V o c a l o i d Vocaloid Vocaloid买回了家。打开之后,你发现界面是一个长为 n n n的序列,代表音调,并形成了全排列。你看不懂日语,经过多次尝试,你只会用一个按钮:将一段区间按升序排序。不理解音乐的 H e r c i e r Hercier Hercier决定写一个脚本,进行 m m m次操作,每次对一段区间进行操作。可惜 H e r c i e r Hercier Hercier不会写脚本,他找到了在机房里的你,请你模拟出最后的结果。

数据范围:

Analysis:

看这数据范围就知道复杂度跟 n n n有很大关系。
每一次对区间排序,实际上不管怎么样排,总的排序次数是不变的。考虑这样的排序,每次交换一对相邻的数,其满足 a i > a i + 1 a_i>a_{i+1} ai>ai+1。不难发现这样的排序次数是 n 2 n^2 n2级别的,因为逆序对个数最多为 n ∗ ( n − 1 ) / 2 n*(n-1)/2 n(n1)/2个。因为每一次对区间排序实际上就是对区间内每一个数执行这个操作,和对整个序列排序没有本质区别,我们只要维护 a i > a i + 1 a_i>a_{i+1} ai>ai+1的位置即可,用 s e t set set去维护,每一次找到最靠近 l l l的地方暴力修改,复杂度 O ( ( n 2 + m ) log ⁡ n ) O((n^2+m)\log{n}) O((n2+m)logn)

Code:

# include<cstdio>
# include<cstring>
# include<algorithm>
# include<set>
using namespace std;
# define ins insert
const int N = 2e3 + 5;
set <int> q;
set <int>::iterator it;
int a[N];
int n,m,L,R;
int main()
{
	freopen("miku.in","r",stdin);
	freopen("miku.out","w",stdout);
	scanf("%d%d%d%d",&n,&m,&L,&R);
	for (int i = 1 ; i <= n ; ++i) { scanf("%d",&a[i]); if (a[i - 1] > a[i]) q.ins(i - 1); }
	while (m--)
	{
		int l,r; scanf("%d%d",&l,&r);
		it = q.lower_bound(l);
		while (it != q.end() && (*it) < r)
		{
			it = q.lower_bound(l); int p = *it;
			q.erase(it); swap(a[p],a[p + 1]);
			if (a[p + 1] > a[p + 2]) q.ins(p + 1);
			if (a[p - 1] < a[p + 1] && a[p - 1] > a[p]) q.ins(p - 1);
			it = q.lower_bound(l);
		}
	}
	for (int i = L ; i <= R ; ++i) printf("%d ",a[i]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值