CF1157 E. Minimum Array

题目传送门:https://codeforces.com/problemset/problem/1157/E

题目大意:

给两个长度为\(n\)的序列\(A,B\),令\(C_i=(A_i+B_i)\%n\),求对\(B\)序列任意排序后,所能得到的字典序最小的\(C\)序列


从头到尾考虑每一个\(A_i\),按\(n-A_i,n-A_i+1,...,n-1,0,...,n-A_i-1\)的顺序,取能取到的数,放在\(B_i\)即可

可用multiset维护,也可以用线段树维护

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair<int,int>
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
template<typename T>inline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=2e5;
int pos[N+10];
struct S1{
	#define ls (p<<1)
	#define rs (p<<1|1)
	int Tree[(N<<2)+10];
	void update(int p){Tree[p]=min(Tree[ls],Tree[rs]);}
	void Build(int p,int l,int r){
		if (l==r){
			Tree[p]=pos[l]?l:int_inf;
			return;
		}
		int mid=(l+r)>>1;
		Build(ls,l,mid);
		Build(rs,mid+1,r);
		update(p);
	}
	void Delete(int p,int l,int r,int x){
		if (l==r){
			if (!--pos[l])	Tree[p]=int_inf;
			return;
		}
		int mid=(l+r)>>1;
		if (x<=mid)	Delete(ls,l,mid,x);
		if (x>mid)	Delete(rs,mid+1,r,x);
		update(p);
	}
	int Query(int p,int l,int r,int L,int R){
		if (L<=l&&r<=R)	return Tree[p];
		int mid=(l+r)>>1,res=int_inf;
		if (L<=mid)	res=min(res,Query(ls,l,mid,L,R));
		if (R>mid)	res=min(res,Query(rs,mid+1,r,L,R));
		return res;
	}
	#undef ls
	#undef rs
}ST;//Segment Tree
int A[N+10],B[N+10];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int n=read(0);
	for (int i=1;i<=n;i++)	A[i]=read(0);
	for (int i=1;i<=n;i++)	pos[B[i]=read(0)+1]++;
	ST.Build(1,1,n);
	for (int i=1;i<=n;i++){
		int delta=(n-A[i])%n;
		int x=ST.Query(1,1,n,delta+1,n);
		if (x==int_inf)	x=ST.Query(1,1,n,1,delta);
		printf("%d%c",(x-1+A[i])%n,i==n?'\n':' ');
		ST.Delete(1,1,n,x);
	}
	return 0;
}

CF1157

posted @ 2021-07-01 21:10  Wolfycz  阅读(19)  评论(0编辑  收藏  举报