跳到正文

题解

CF2041J Bottle Arrangement 题解

Gold14526约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年11月26日
收录
2026年11月23日
更新
2026年11月23日
篇幅
约 6 分钟
获赞
6
题目
CF2041J

版本

  1. v1当前

Problem:\rm Problem:Bottle Arrangement

Difficulty:2700\rm Difficulty:2700

题意

给出长度为 nn 的两个序列 a,ba,b,要求给 bb 序列中的若干个数减一并重排,设重排后的序列为 b′b',要求 b′b' 满足:

  • b′b' 是个单峰序列。
  • bi′<aib'_i<a_i。

求减一次数的最小值,或报告无解。

1≤n≤5×1051\le n\le5\times10^5

1≤ai,bi≤1091\le a_i,b_i\le10^9

做法

先将 bb 序列从小到大排序,接下来的 bb 序列指从小到大排序后的 bb 序列。

考虑扣出序列最小值 mnmn 的所有位置 p1,p2,...,pkp_1,p_2,...,p_k。

我们可以枚举单峰序列的峰在哪个 [pi,pi+1][p_i,p_{i+1}] 中(或者是 [1,p1][1,p_1] 或 [pk,n][p_k,n]),设 len=pi+1−pi−1len=p_{i+1}-p_i-1,则我们可以贪心的将 bb 序列的前 n−lenn-len 个数放在 [1,pi],[pi+1,n][1,p_i],[p_{i+1},n] 中,所需减 11 的次数就是 bb 序列前 n−lenn-len 个数中 mnmn 的个数,不过如果 bn−lenb_{n-len} 已经大于 mnmn 那么说明这种操作是不合法的,必须选择其他的 [pi,pi+1][p_i,p_{i+1}]。

于是我们只需解决在 [pi+1,pi+1−1][p_i+1,p_{i+1}-1] 中放入 bb 序列的后 lenlen 个数的子问题,做法与上面类似,只需不断递归分治即可。

分治时间复杂度为 O(n)O(n),但是由于要找 pp 序列,需要用一些数据结构维护一下,所以时间复杂度为 O(nlog⁡n)O(n\log n)。

代码

(本人大码量体质,实际上代码写得很清晰)

C++
#include<bits/stdc++.h>
#define cint const int
#define iint inline int
#define uint unsigned int
#define iuint inline unsigned int
#define cuint const unsigned int
#define ll long long
#define cll const long long
#define ill inline long long
#define ull unsigned long long
#define iull inline unsigned long long
#define cull const unsigned long long
#define sh short
#define csh const short
#define ish inline short
#define ush unsigned short
#define iush inline unsigned short
#define cush const unsigned short
using namespace std;
iint read()
{
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch-'0');
		ch=getchar();
	}
	return x;
}
cint inf=1e9;
int n;
int a[500001],b[500001];
namespace T{//segment tree
	struct node{
		int l,r,mn;
	}t[2000001];
	void Build(cint p,cint l,cint r)
	{
		t[p].l=l;
		t[p].r=r;
		if(l==r)
		{
			t[p].mn=a[l];
			return;
		}
		cint mid=l+r>>1;
		Build(p<<1,l,mid);
		Build(p<<1|1,mid+1,r);
		t[p].mn=min(t[p<<1].mn,t[p<<1|1].mn);
	}
	void build()
	{
		Build(1,1,n);
	}
	int Ask(cint p,cint l,cint r)
	{
		if(t[p].l>r||t[p].r<l)return inf;
		if(t[p].l>=l&&t[p].r<=r)return t[p].mn;
		return min(Ask(p<<1,l,r),Ask(p<<1|1,l,r));
	}
	int ask(cint l,cint r)
	{
		return Ask(1,l,r);
	}
	int Find(cint p,cint l,cint r,cint x)
	{
		if(t[p].l>r||t[p].r<l)return -1;
		if(t[p].l>=l&&t[p].r<=r&&t[p].mn!=x)return -1;
		if(t[p].l==t[p].r)return t[p].l;
		int L=Find(p<<1,l,r,x);
		if(L!=-1)return L;
		return Find(p<<1|1,l,r,x);
	}
	int find(cint l,cint r,cint x)
	{
		return Find(1,l,r,x);
	}
}
int find(cint l,cint r,cint x)
{
	cint mid=l+r+1>>1;
	if(l>=r)return mid;
	if(b[mid]<x)return find(mid,r,x);
	return find(l,mid-1,x);
}
int solve(cint l,cint r)
{
	if(l>r)return 0;
	int mn=T::ask(l,r);
	vector<int>p;
	int w=l;
	p.push_back(l-1);
	while(1)
	{
		w=T::find(w,r,mn);
		if(w==-1)break;
		p.push_back(w);
		++w;
	}
	int lst=find(0,n,mn);
	p.push_back(r+1);
	int ans=inf;
	for(int i=0;i<p.size()-1;++i)
	{
		cint pl=p[i]+1,pr=p[i+1]-1;
		cint len=pr-pl+1,tp=n-len;
		if(b[tp]>mn)continue;
		if(tp<=lst)
		{
			ans=min(ans,solve(pl,pr));
		}
		else
		{
			ans=min(ans,solve(pl,pr)+tp-lst);
		}
	}
	return ans;
}
int main()
{
	n=read();
	for(int i=1;i<=n;++i)
	{
		a[i]=read();
	}
	for(int i=1;i<=n;++i)
	{
		b[i]=read();
	}
	T::build();
	sort(b+1,b+n+1);
	int ans=solve(1,n);
	printf("%d",(ans==inf?-1:ans));
	return 0;
}
原文