返回顶部

Buy Tickets

Buy Tickets

  • Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…
    The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.
    It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!
    People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

  • Input
    There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

    • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
    • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.
      There no blank lines between test cases. Proceed to the end of input.
  • Output
    For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

    这题可以用线段树,也可用树状数组
    需要逆向思维,倒着看,因为最后一个往往是确定的

image

val存的是在这一段中有几个空位

image

例如存放一个2 848439这一组数

image

线段树代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
int ans[200005];
const int N = 200010;
struct ac
{
	int x,val;
}a[N];
struct node
{
    int id,st,en,val;
}st[N<<2];
void bt(int rt,int L,int R)
{
    st[rt].st = L;
    st[rt].en = R;
    st[rt].val = R - L +1;
    if(L == R) return ;
    int mid = (L + R)>>1;
    bt(rt<<1, L, mid);
    bt(rt<<1|1, mid+1, R);
}
int query(int rt,int pos)
{
    st[rt].val--;
    if(st[rt].st == st[rt].en) return st[rt].st;
    if(pos <= st[rt<<1].val) return query(rt<<1, pos);
    else return query(rt<<1|1, pos - st[rt<<1].val);
}
int main()
{
//	ios_base::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].val);
		}
		bt(1,1,n);
		for(int i=n;i>=1;i--)
		{
			int t=query(1,a[i].x+1);
			ans[t]=a[i].val;
			
		}
		for(int i=1;i<=n;i++)
		{
			printf("%d ",ans[i]);
		}
		putchar('\n');
	}
	return 0;
}

空位初始值为1,有人则初始值为0.所以我们要求前缀和为p[n]的最小值。树状数组可以在O(logn)时间求出前缀和,然后我们只需要二分查找即可,时间也是O(logn).

树状数组
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
int c[200005],ans[200005];
struct ac
{
	int x,val;
}a[200005];
int lowbit(int x)
{
	return x&(-x);
}
void update(int x,int val)
{
	while(x<=n)
	{
		c[x]+=val;
		x+=lowbit(x);
	}
}
int ask(int x)
{
	int ans=0;
	while(x)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
int find(int x)
{
	int l=1,r=n;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(ask(mid)<x)
		{
			l=mid+1;
		}else
		{
			r=mid-1;
		}
	}
	return l;
}
int main()
{
	while(~scanf("%d",&n))
	{
		memset(c,0,sizeof(c));
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].val);
			update(i,1);
		}
//		bt(1,1,n);
		for(int i=n;i>=1;i--)
		{
			int t=find(a[i].x+1);
			ans[t]=a[i].val;
			update(t,-1);
		}
		for(int i=1;i<=n;i++)
		{
			printf("%d ",ans[i]);
		}
		putchar('\n');
	}
	return 0;
}
posted @ 2024-03-08 11:39  wlesq  阅读(9)  评论(0编辑  收藏  举报