跳到正文

个人记录

题解 ICPC2022HongKong L 【Permutation Compression】

xukuan约 7 分钟
原文
文章信息与版本

文章信息

发布
2023年6月27日
收录
2026年10月4日
更新
2026年11月11日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

zju的官方题解如下:

First we can notice that we can delete elements one by one from large to small. Assume the i-th element deleted is did_i, then if di>di+1d_i>d_{i+1}, we can swap did_i and di+1d_{i+1}, the deleting sequence is still legal.

Now Let’s consider elements from large to small, if i is not deleted, then none of the further operating intervals can cover i, splitting the sequence into two sequences. Otherwise if the sequence which i is in have x remaining elements, then we have to perform an operation with li≤xl_i \leq x.

This can be maintained with std::set and fenwick tree. We just have to store all such x and check if they can pair with given lil_i, this can be solved greedily. The time complexity is O(nlogn)O(n log n).

我的实现:

预处理出每一个数左边和右边的数种比它大的之中最离他最近的,记为prevmin和nextmin,如果没有,记为0和n+1

然后对需要删掉的数从大到小,用树状数组统计在(premin,nextmin) 之中已经被删掉的数的个数,算出能支持的最大的区间长度(端点算不算影响不大)

最后看区间长度是否合法

代码:

C++
//by xukuan
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;

const ll N=200010,INF=1ll<<60;
ll T,n,m,k,a[N],b[N],l[N],flag[N],Prevmin[N],Nextmin[N],ans[N],tot;
vector<ll> st;
struct cmp{
	inline bool operator ()(ll x,ll y){
		return a[x]<a[y];
	}
};
priority_queue<ll,vector<ll>,cmp> q;

struct Tree{
	ll sum[N];
	Tree(){
		for(ll i=1; i<=n+1; i++) sum[i]=0;
 	}
	ll lowbit(ll x){
		return x&(-x);
	}
	ll query(ll x){
		ll ans=0;
		for(ll i=x; i; i-=lowbit(i)){
			ans+=sum[i];
		}
		return ans;
	}
	ll query(ll l,ll r){
		return query(r)-query(l);
	}
	void update(ll x,ll val){
		for(ll i=x; i<=n+1; i+=lowbit(i)){
			sum[i]+=val;
		}
	}
}tr;

inline ll read(){
	ll x=0,tmp=1;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-') tmp=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return tmp*x;
}

inline void clean(){
	for(ll i=1; i<=n; i++) flag[i]=0;
	while(!st.empty()) st.pop_back();
	tot=0;
}

inline void NO(){
	printf("NO\n");
}

inline void YES(){
	printf("YES\n");
}

inline ll find(ll val){
	ll l=0,r=(ll)st.size()-1,ans=-1;
	while(l<=r){
		ll mid=(l+r)>>1;
		if(a[st[mid]]>val){
			ans=mid;
			l=mid+1;
		}
		else r=mid-1;
	}
	return ans==-1?INF:st[ans];
}

inline void solve(){
	n=read(); m=read(); k=read();
	for(ll i=1; i<=n; i++) a[i]=read();
	for(ll i=1; i<=m; i++) b[i]=read();
	for(ll i=1; i<=k; i++) l[i]=read();
	sort(l+1,l+1+k);
	
	if(n>m+k){
		NO();
		return;
	}
	
	clean();
	ll now=1;
	for(ll i=1; i<=n&&now<=m; i++){
		if(a[i]==b[now]){
			flag[i]=1;
			now++;
		}
	}
	if(now!=m+1){
		NO();
		return;
	}
	
	while(!st.empty()) st.pop_back();
	for(ll i=1; i<=n; i++){
		if(flag[i]) while(!st.empty()&&a[st.back()]<a[i]) st.pop_back();
		if(!st.empty()) Prevmin[i]=find(a[i]);
		else Prevmin[i]=0;
		if(Prevmin[i]==INF) Prevmin[i]=0;
		if(flag[i]) st.push_back(i);
	}
	while(!st.empty()) st.pop_back();
	for(ll i=n; i>=1; i--){
		if(flag[i]) while(!st.empty()&&a[st.back()]<a[i]) st.pop_back();
		if(!st.empty()) Nextmin[i]=find(a[i]);
		else Nextmin[i]=n+1;
		if(Nextmin[i]==INF) Nextmin[i]=n+1;
		if(flag[i]) st.push_back(i);
	} 
//	for(ll i=1; i<=n; i++) cout<<Prevmin[i]<<' '<<Nextmin[i]<<endl;
	
	for(ll i=1; i<=n; i++){
		if(!flag[i]) q.push(i);
	}
	Tree tr;
	while(!q.empty()){
		ll x=q.top(); q.pop();
		ans[++tot]=Nextmin[x]-Prevmin[x]-1-tr.query(Prevmin[x],Nextmin[x]);
//		cout<<a[x]<<' '<<tr.query(Prevmin[x],Nextmin[x])<<endl;
		tr.update(x,1);
	}
	sort(ans+1,ans+1+tot);
	for(ll i=1; i<=tot; i++){
		if(ans[i]<l[i]){
			NO();
			return;
		}
	}
	YES();
}

int main(){
	T=read();
	while(T--) solve();	
	return 0;
}
原文
存档评论2 条抓取于