首页 > 其他分享 >POJ - Buy Tickets

POJ - Buy Tickets

时间:2023-08-01 15:33:30浏览次数:42  
标签:Tickets Buy int queue person POJ include was pl

Smiling & Weeping

                    ----你看这个人,嘴里说着喜欢我

                      却又让我这么难过

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
思路:我们要学会逆序化的思维方式,我们插入数据(看似是一道普通的链表题目,但是你会发现链表的查询个数很耗时间)使用链表会在查询插队位置上
消耗大量的时间TLE,所以我们要学会使用逆序化的思维方式,最后插入的元素是直接排在第x人的后面(最后插入的人位置永远是固定的),那我们就可以
倒序找前面是否有x个空位置(没人设置为1,有人设置为0)
题目链接:2828 -- Buy Tickets (poj.org)
Talk is cheap , Show me your code
 1 //我们要学会逆向的思维方式,最后出现的一定是位置确定的
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<vector>
 8 using namespace std;
 9 #define ls(p) p<<1
10 #define rs(p) p<<1|1
11 const int maxn = 200005;
12 int tree[maxn<<2] , num[maxn] , n;
13 void push_up(int p)
14 {
15     tree[p] = tree[ls(p)] + tree[rs(p)];
16 }
17 void build(int p, int pl , int pr)
18 {
19     if(pl == pr)
20     {
21         tree[p] = 1;
22         num[pl] = 0;
23         return ;
24     }
25     int mid = pl+pr >> 1;
26     build(ls(p) , pl , mid);
27     build(rs(p) , mid+1 , pr);
28     push_up(p);
29 }
30 void update(int L ,int R , int p, int pl , int pr , int ind , int v)
31 {
32     if(pl == pr)
33     {
34         tree[p] = 0;
35         num[pl] = v;
36         return ;
37     }
38     int mid = pl+pr >> 1;
39     if(ind <= tree[ls(p)]) update(L , R , ls(p) , pl , mid , ind , v);
40     else
41         update(L , R , rs(p) , mid+1 , pr , ind-tree[ls(p)] , v);
42     push_up(p);
43 }
44 int main()
45 {
46     while(scanf("%d",&n) != EOF)
47     {
48         int a , b;
49         build(1,1,n);
50         vector<pair<int , int> > p;
51         for(int i = 1; i <= n; i++)
52         {
53             scanf("%d%d",&a,&b);
54             p.push_back(make_pair(a+1,b));
55         }
56         for(int i = n-1; i >= 0; i--)
57             update(1 , n , 1 ,1 , n , p[i].first , p[i].second);
58         for(int i = 1; i <= n; i++)
59             printf("%d ",num[i]);
60         printf("\n");
61     }
62     return 0;
63 }

山海自有归期,风雨自有相逢

难过(ಥ﹏ಥ)

标签:Tickets,Buy,int,queue,person,POJ,include,was,pl
From: https://www.cnblogs.com/smiling-weeping-zhr/p/17596660.html

相关文章

  • POJ 1548 Robots
    \(POJ\)\(1548\)\(Robots\)题意相当于给出\(N\)个坐标点,因为机器人只能向下或者向右走,所以如果能到达其他点,则连接这两个点,即line[i][j]=1最小路径覆盖数:对于一个\(DAG\)(有向无环图),选取最少条路径,使得每个顶点属于且仅属于一条路径。路径长度可以为零;(有向图中找一些路径,使......
  • POJ1904 King's Quest
    \(POJ1904\)\(King's\)\(Quest\)一、题目描述有\(n\)个王子,每个王子都有\(k\)个喜欢的妹子,每个王子只能和喜欢的妹子结婚。现有一个匹配表,将每个王子都与一个自己喜欢的妹子配对。请你根据这个表得出每个王子可以和几个自己喜欢的妹子结婚,按序号升序输出妹子的编号,这个表应满......
  • poj 2886 Who Gets the Most Candies? (线段树单点更新应用)
                           poj2886WhoGetstheMostCandies?DescriptionNchildrenaresittinginacircletoplayagame.Thechildrenarenumberedfrom1toNinclockwiseorder.Eachofthemhasacardwithanon-zerointegeronit......
  • POJ 3694 Network
    POJ3694Network一、题目大意\(n\)个点,\(m\)个边,连通图。点与点之间通过边连接,如果切断某个边使得有点与其他点连接断开(连通分支增加),则称这种边为桥梁(离散上叫割边)。接下来有\(Q\)个操作,每操作一次,也就是切断某条边后,输出当前存在的桥梁数量。二、样例分析我们看这个4......
  • (Relax 数论1.26)POJ 1496 Word Index(计算一个字符串在字典中的位置)
    大致题意:(与POJ1850基本一致)输出某个str字符串在字典中的位置,由于字典是从a=1开始的,因此str的位置值就是在str前面所有字符串的个数+1规定输入的字符串必须是升序排列。不降序列是非法字符串要求用循环输入,输入若干组字符串,若输入非法字符串则输出0,但不结束程序,这是和POJ1850......
  • POJ 1458 Common Subsequence(动态规划)
    传送门代码如下:#include<iostream>#include<cstdio>usingnamespacestd;intmaxLen[1000][1000];intmain(){strings1,s2;while(cin>>s1>>s2){intlength1=s1.length();intlength2=s2.length();......
  • 题解 POJ3318【Matrix Multiplication】
    postedon2022-10-2119:56:08|under题解|sourceproblem判断三个\(n\timesn\)的矩阵是否满足\(A\timesB=C\),\(n\leq500\)。solution随机一个行向量\(v\)。若\(a\timesb=c\),则有\(v\timesa\timesb=v\timesc\)(不充分)。显然相乘复杂度仅为\(O(n^2)\)。类似于......
  • poj 1716 Integer Intervals (贪心)
    题意:给定n个连续的区间,求一个集合。其中,n个区间的每一个区间都至少包含两个这个集合的元素。求这个集合元素的最小值。 题解:1、先将所有区间按终点从小到大排序。2、我们先取第一个区间(排好序的区间)的两个值,因为要使得结果最优,我们肯定选择最接近终点的那两个值。假设一个为Selem,......
  • poj 1844 sum (数学)
    题意:给出一个数S,从1到N个数,每个数前面可以是负号或者是正号,这样累加起来,结果可以等于S,问最小的N是多少。题解:因为从1一直加到n的值(假设为sum(n))等于sum的n是最小的。所以我们先算出sum(n)大于等于sum的那个n。这样我们可以得出一个值m=sum(n)-sum.如果m==0那么n就是我们要求的......
  • poj 2311 Cutting Game (sg函数)
    小记:这题是对sg函数的初步理解。对于sg函数只要g[x]==0,则此时为必败态。x作为后继,我们就要对所有的后继进行标记,然后mex之。因为每次只能切一刀,所以切完之后,会有两块方格,而对每一块方格进行游戏又会有一个sg函数值,所以根据sg函数的性质,它这一刀所代表的后继,即为这两块方格的sg函......