题意是不断有人在插队,然后问你最后排列是什么样子。
思路很重要,想了很久还是没想出来,瞄了一眼题解看到个从后往前。想了一会顿时明白了。。因为首先最后一个人是肯定能得到他所要的位置的,倒数第二个人要占的位置假设是第2个是除去最后一人占的位置也就是说空位的第二个,然后以此类推,归纳来说就是当前人最后占的位置是所剩下空位的第(他要占的位置)个。
写起来很好写。
AC代码 :
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<string.h>
#include<string>
#include<bitset>
using namespace std;
#define ll __int64
#define eps 1e-8
#define NMAX 200005
#define MOD 10007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
int T[4*NMAX][2],pos,nct,n,a[NMAX][2],ans[NMAX];
void build(int l, int r, int rt)
{
if(l == r)
{
T[rt][0] = 1;
T[rt][1] = nct++;
return;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
T[rt][0] = T[rt<<1][0]+T[rt<<1|1][0];
}
void findit(int x, int l, int r, int rt)
{
if(l == r)
{
T[rt][0] = 0;
pos = rt;
return;
}
int mid = (l+r)>>1;
if(T[rt<<1][0] >= x) findit(x,lson);
else findit(x-T[rt<<1][0],rson);
T[rt][0] = T[rt<<1][0]+T[rt<<1|1][0];
}
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o4.txt","w",stdout);
#endif // GLQ
while(~scanf("%d",&n))
{
nct = 1;
build(1,n,1);
for(int i = 0; i < n; i++)
{
scanf("%d%d",&a[i][0],&a[i][1]);
a[i][0]++;
}
for(int i = n-1; i >= 0; i--)
{
findit(a[i][0],1,n,1);
int t = pos;
// cout<<a[i][0]<<" "<<t<<" "<<T[t][1]<<endl;
ans[T[t][1]] = a[i][1];
}
for(int i = 1; i <= n; i++) printf("%d%c",ans[i],(i==n)?'\n':' ');
}
return 0;
}