Queue HDU - 5493(线段树||树状数组+二分)

NN people numbered from 1 to NN are waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now, so they decide to go out and have lunch first. When they get back, they don’t remember the exact order of the queue. Fortunately, there are some clues that may help.
Every person has a unique height, and we denote the height of the ii-th person as hihi. The ii-th person remembers that there were kiki people who stand before him and are taller than him. Ideally, this is enough to determine the original order of the queue uniquely. However, as they were waiting for too long, some of them get dizzy and counted kiki in a wrong direction. kiki could be either the number of taller people before or after the ii-th person.
Can you help them to determine the original order of the queue?
Input
The first line of input contains a number TT indicating the number of test cases (T≤1000T≤1000).
Each test case starts with a line containing an integer NN indicating the number of people in the queue (1≤N≤1000001≤N≤100000). Each of the next NN lines consists of two integers hihi and kiki as described above (1≤hi≤109,0≤ki≤N−11≤hi≤109,0≤ki≤N−1). Note that the order of the given hihi and kiki is randomly shuffled.
The sum of NN over all test cases will not exceed 106106
Output
For each test case, output a single line consisting of “Case #X: S”. XX is the test case number starting from 1. SS is people’s heights in the restored queue, separated by spaces. The solution may not be unique, so you only need to output the smallest one in lexicographical order. If it is impossible to restore the queue, you should output “impossible” instead.
Sample Input
3
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1
Sample Output
Case #1: 20 10 30
Case #2: 10 20 30
Case #3: impossible
又是排队问题。排队问题用线段树和树状数组的比较多。
题意:给你n个人的身高,和他们前面或者后面有k个人比他高,让你构造出这样的一个序列。但是需要字典序最小。
思路:既然需要字典序最小,所以我们需要把身高有小到大排序,身高小的在前面。什么时候不能构造出这样的序列呢?假如第i个人前面或者后面有k个人比他高,但是现在有n-i+1个 人没有安排,假如n-i+1<=k,那么怎么也不可能安排出这样的一组序列。这样的话,就是不可能。在有可能安排出这样的序列的时候,我们尽量把小的安排在前面。
假如前面有k个人,那么这个人前面一定要有k个空位安排比他高的人。
假如后面有k个人,那么这个人前面一定要有n-i+1-k个位置安排比他高的人。
比较这两个位置,谁小就把这个人安排在这个位置。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
struct node{
	int l;
	int r;
	int sum;
}p[maxx<<2];
struct Node{
	int val;
	int id;
	bool operator<(const Node &a)const{
		return a.val>val;
	}
}e[maxx];
int n,ans[maxx];

inline void pushup(int cur)
{
	p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum;
}
inline void build(int l,int r,int cur)//一开始每个位置没有人的时候就把这个位置设置为1.
{
	p[cur].l=l;
	p[cur].r=r;
	if(l==r)
	{
		p[cur].sum=1;
		return ;
	}
	int mid=l+r>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur);
}
inline void update(int num,int v,int cur)//我们把这个人安排在某个位置的时候,这个位置前面有可能已经有某些位置有人了。但是我们还是要保证它前面有num个空位。
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(L==R)
	{
		ans[L]=v;
		p[cur].sum=0;
		return ;
	}
	if(p[cur<<1].sum>=num) update(num,v,cur<<1);
	else update(num-p[cur<<1].sum,v,cur<<1|1);
	pushup(cur);
}
int main()
{
	int t,x,k=0,y;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d%d",&e[i].val,&e[i].id);
		printf("Case #%d: ",++k);
		sort(e+1,e+1+n);
		int flag=1;
		build(1,n,1);
		for(int i=1;i<=n;i++)
		{
			x=e[i].id;
			y=n-i+1;//还剩y个人没有安排
			if(x>=y){flag=0;break;}
			if(n-i+1-x>x+1) update(x+1,e[i].val,1);//安排在尽量靠前的位置
			else update(n-i+1-x,e[i].val,1);
		}
		if(!flag) printf("impossible\n");
		else for(int i=1;i<=n;i++) printf("%d%c",ans[i],i==n?'\n':' ');
	}
}

这是匡斌带你飞的二分专题中的题目。
二分的时候,我么二分那个位置。我们还是要尽量安排在前面。所以树状数组需要找出一个位置,它前面的空位大于那两种情况中小的那一种情况。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
struct node{
	int val;
	int id;
	bool operator <(const node &a)const{
		return a.val>val;
	}
}p[maxx];
int c[maxx],ans[maxx];
int n;

inline int lowbit(int x){return x&(-x);}
inline void update(int cur,int v){while(cur<maxx) c[cur]+=v,cur+=lowbit(cur);}
inline int query(int cur){int sum=0;while(cur>0) sum+=c[cur],cur-=lowbit(cur);return sum;}

inline void solve(int i)
{
	int x=p[i].val;
	int y=p[i].id;
	int l=1,r=n,mid,z=1;
	while(l<=r)
	{
		mid=l+r>>1;
		if(query(mid)<min(y+1,n-i+1-y))//大于小的那一种情况,前面k个人或者后面k个人。
		{
			l=mid+1;
		}
		else r=mid-1,z=mid;
	}
	ans[z]=x;
	update(z,-1);
}
int main()
{
	int t,x,y,k=0;
	scanf("%d",&t);
	while(t--)
	{
		memset(c,0,sizeof(c));
		scanf("%d",&n); 
		for(int i=1;i<=n;i++) update(i,1);
		for(int i=1;i<=n;i++) scanf("%d%d",&p[i].val,&p[i].id);
		sort(p+1,p+1+n);
		int flag=1;
		printf("Case #%d: ",++k);
		for(int i=1;i<=n;i++)
		{
			x=p[i].id;
			y=n-i+1;
			if(x>=y) {flag=0;break;}
			else solve(i);
		}
		if(!flag) printf("impossible\n");
		else for(int i=1;i<=n;i++) printf("%d%c",ans[i],i==n?'\n':' ');
	}
	return 0;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值