poj 2828 Buy Tickets

    xiaoxiao2025-02-05  12

    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 4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31492 Sample Output 77 33 69 51

    31492 20523 3890 19243

    采用倒序插入,pos的意义就是找到一个位置,它的前面刚好有pos个空位,用一个empty数组记录区间[l,r]之间有多少个空位,然后进来一个p,比较左右子树的空位数,如果坐标的空位数 >= p,那么说明p应该放在左子树,否则,p应该放右子树,并且p还要减去左子树的空位数,因为右子树的空位数也是从0开始的。

    推荐一个博客网站http://www.cnblogs.com/wally/p/3614721.html

    #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> using namespace std; #define ll long long #define MAX 1<<<30 #define lson rt<<1 #define rson rt<<1|1 #define getMin(a,b) a<b?a:b #define getMax(a,b) a>b?a:b const int N=200005; int epy[N<<2]; int n,index,pos[N],id[N],ans[N]; void init(int l,int r,int rt) { epy[rt]=r-l+1; // printf("rt:%d epy:%d r:%d l:%d\n",rt,epy[rt],r,l); if (l==r) return; int mid=(l+r)>>1; init(l,mid,lson); init(mid+1,r,rson); } void update(int l,int r,int rt,int p) { epy[rt]--; if (l==r) { index=l; // printf("index:%d\n",index); return; } int mid=(l+r)>>1; if (epy[lson]>=p) update(l,mid,lson,p); else { p-=epy[lson]; update(mid+1,r,rson,p); } } int main() { int i; while (~scanf("%d",&n)) { for (i=1;i<=n;i++) scanf("%d%d",&pos[i],&id[i]); init(1,n,1); for (i=n;i>=1;i--) { update(1,n,1,pos[i]+1); ans[index]=id[i]; } for (i=1;i<n;i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); } return 0; } 这里面有输出语句便于理解线段树的分级走向,对于这个题为什么要采取倒序,因为发生更改的条件是该位置已被前面的人占了,只能往后去找空位,所以要用倒序,至于为什么如果p大于empty[lson],还要减去empty[lson],因为它发生了状态转移,转移到了它的子树上,又相当于从头开始 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson rt << 1 #define rson rt << 1 | 1 const int MAXN = (200000 + 20); int empty[MAXN << 2]; int n, index, pos[MAXN], id[MAXN], ans[MAXN]; void Build(int L, int R, int rt) { empty[rt] = R - L + 1; printf("rt:%d empty:%d\n",rt,empty[rt]); if (L == R) return; int M = (L + R) >> 1; Build(L, M, lson); Build(M + 1, R, rson); } void Update(int L, int R, int rt, int p) { empty[rt]--; printf("//**============**//\n"); printf("rt:%d empty:%d\n",rt,empty[rt]+1); if (L == R) { index = L; printf("index:%d\n",index); return; } int M = (L + R) >> 1; if (empty[lson] >= p) printf("Left\n"),Update(L, M, lson, p); else p -= empty[lson], printf("p:%d lson:%d\nRight\n",p+empty[lson],lson),Update(M + 1, R, rson, p); } int main() { while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) { scanf("%d %d", &pos[i], &id[i]); } Build(1, n, 1); for (int i = n; i >= 1; i--) { Update(1, n, 1, pos[i] + 1); ans[index] = id[i]; } for (int i = 1; i <= n; i++) { printf(i == n ? "%d\n" : "%d ", ans[i]); } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1296125.html
    最新回复(0)