Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 19422 | Accepted: 9616 |
Description
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.
Sample Input
40 771 511 332 6940 205231 192431 38900 31492Sample Output
77 33 69 5131492 20523 3890 19243Hint
The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.
題意:買票,可能會插隊,告訴你來的人的插入的位置和數值。讓你把最后的數值輸出來。
思路:想了好久,沒想到有什么好解決辦法,看了別人的一時半會也不知道看空位數是什么意思。
后來算是慢慢的想明白了。
首先就是,最后插入的那個人位置一定是固定的,所以我們從后面往前插。至于為什么是插空位,我不知道是誰想出來的,確實能過這個題。
看一下它告訴你的事插入的位置和數值。我們重點研究一下插入的位置。我們知道你相同的位置后來的必定是向后走的。
給你一個位置,我們看一下當前區間的空位數,如果插入的位置比當前區間的空位數小,那么往左區間插,(因為左區間的位置小),否則就往右區間插。
注意如果往右區間插,那么是必須要減去左區間的空位的個數的。
因為是倒著來的,所以后面來的位置是固定的,因此我們根據空位數和位置的關系,來找到最終的位置,最后得出答案。
模擬的過程其實就是我們從最后一個開始,確定下位置,扣去,相當于不存在了,剩下的n-1個位置重新排好,然后重復上一步。因為下一個不存在的話對于上一個來說就是當前的位置,是固定的了,就是因為下一個來了,才有可能變動的。而扣去之后的位置的體現其實就是空位的位置。
然后用線段樹的話就能快速定位位置了。
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int MAXN=2e5+7;int n,m;int ans[MAXN];struct node{ int l,r; int sum;}tree[MAXN<<2];int pos[MAXN],x[MAXN];void build_tree(int i,int l,int r){ tree[i].l=l; tree[i].r=r; tree[i].sum=r-l+1; if(l==r)return; int mid=(l+r)>>1; build_tree(i<<1,l,mid); build_tree(i<<1|1,mid+1,r);}int get_ans(int i,int val){ if(tree[i].l==tree[i].r) { tree[i].sum=0; return tree[i].l; } int p; if(val<tree[i<<1].sum)p=get_ans(i<<1,val); else p=get_ans(i<<1|1,val-tree[i<<1].sum); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; return p;}int main(){ while(~scanf("%d",&n)) { build_tree(1,0,n-1); for(int i=0;i<n;++i) { scanf("%d%d",&pos[i],&x[i]); } for(int i=n-1;i>=0;--i) { ans[get_ans(1,pos[i])]=x[i]; } for(int i=0;i<n-1;++i)printf("%d ",ans[i]); printf("%d/n",ans[n-1]); } return 0;}
新聞熱點
疑難解答