codeforces 38G Queue

http://www.elijahqi.win/2018/02/15/codeforces-38g-queue/
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya.
Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n.
Let the queue consist of n - 1 people at the moment. Let’s look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: “Erm… Excuse me please but it’s very important for me… could you please let me move up the queue?”), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times.
In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place.
Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops.
Input
The first input line contains an integer n which is the number of people who has joined the queue (1 ≤ n ≤ 105). In the next n lines descriptions of the people are given in order of their coming — space-separated integers ai and ci (1 ≤ ai ≤ n, 0 ≤ ci ≤ n). Every description is located on s single line. All the ai’s are different.
Output
Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space.
Examples
Input
2
1 0
2 1
Output
2 1
Input
3
1 3
2 3
3 3
Output
3 2 1
Input
5
2 3
1 4
4 3
3 1
5 2
Output
3 1 5 4 2
昨天路途上又奔波了很久 还被拉去打球了 ..本来想偷懒的 这题直接看看题解得了 然后发现题解都看不懂了 有的题解说二分?自己yy了一下觉得不是很对啊 zhx巨佬的题解也看不太懂了 只好半夜自己冷静想一想怎么搞
题意:按照题目给定的顺序进人 每个人一开始站在队伍的最末端 然后他有一个重要值a 有一个良心值c 即插队次数(交换次数<=c 如果前面的那个人重要值小于当前这个 并且这个人良心值没有变0 那么他就会和前面一个人交换 直到 良心值为0或者是前一个人比他重要那么停止交换
我的做法是 首先我先算出来我这个人最多可以和前面几个人进行交换 然后把这段后缀在splay上转出来 然后 同时在splay上维护一个最大值 然后直接取splay上找一下 第一个比我大的即可 如果没有 那么就把位置标记为我良心值能换到的最前面的地方 好直接把我这个人插进去即可

#include<cstdio>
#include<algorithm>
#define N 110000
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S) {T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int fa[N],mx[N],c[N][2],v[N],size[N],n,pos,cnt,root,id[N];
inline void update(int x){
    size[x]=size[c[x][0]]+size[c[x][1]]+1;
    mx[x]=max(max(mx[c[x][0]],mx[c[x][1]]),v[x]);
}
inline void rotate(int x,int &tar){
    int y=fa[x],z=fa[y];
    if (y==tar) tar=x;else c[z][c[z][1]==y]=x;
    int l=c[y][1]==x,r=l^1;
    fa[c[x][r]]=y;fa[y]=x;fa[x]=z;
    c[y][l]=c[x][r];c[x][r]=y;update(y);update(x);
}
inline void splay(int x,int &tar){
    while(x!=tar){
        int y=fa[x],z=fa[y];
        if (y!=tar){
            if(c[y][0]==x^c[z][0]==y) rotate(x,tar);else rotate(y,tar);
        }rotate(x,tar);
    }
}
inline int find(int x,int sz){
    int l=c[x][0],r=c[x][1];
    if (size[l]+1==sz) return x;
    if (sz<=size[l]) return find(l,sz);else return find(r,sz-size[l]-1);
}
inline void check(int x,int vv,int sz){
    if (!x) return;int l=c[x][0],r=c[x][1];
    if (mx[r]>vv) check(r,vv,sz+size[l]+1);
    else if (v[x]>vv) pos=sz+size[l]+1;
    else if (mx[l]>vv) check(l,vv,sz);
}
inline void print(int x){
    if (c[x][0]) print(c[x][0]);
    if (id[x]) printf("%d ",id[x]);
    if (c[x][1]) print(c[x][1]);
}
inline void print1(int x){
    if (c[x][0]) print1(c[x][0]);
    if (v[x]) printf("%d ",v[x]);
    if (c[x][1]) print1(c[x][1]);
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();size[++cnt]=2;root=cnt;
    c[root][0]=++cnt;fa[cnt]=root;size[cnt]=1;
    for (int i=1;i<=n;++i){
        int a=read(),cc=read();
        pos=0;int l=max(1,i-cc);int xx=find(root,l),yy=find(root,i+1);
        splay(yy,root);
        splay(xx,c[root][0]);
        check(c[xx][1],a,0);++l;//print1(c[xx][1]);puts("as");
        if(pos) l=l+pos;xx=find(root,l-1),yy=find(root,l);
        splay(yy,root);splay(xx,c[root][0]);size[++cnt]=1;fa[cnt]=xx;
        c[xx][1]=cnt;v[cnt]=mx[cnt]=a;update(xx);update(root);id[cnt]=i;
    //  print1(root);puts("");puts("");
    }print(root);//print(root);puts("");puts("");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值