Poj2828 (buy tickets)

Source: Internet
Author: User

Topic address: Buy tickets

 

Question:

Simulate buying tickets for a queue. There are n people, each of whom has a pos to be inserted to the back of the nth person, and there is a Val value to update the order of N queues, after the insertion is complete, the Val value is output in the order of teams.

 

Solution:

Directly simulate insertion. It will not work. It will be TLE. If insertion is in progress, the next person will be placed in the front, and all the inserted POS locations will be moved back, the time is too complex. At this time, it is better to reduce the time if you insert data from the back to the back. Inserting the POS of the person from the back must be the position he wants, and it must be correct. If the POs position of the last person is 0, it means that he must be inserting it at the beginning, if the POS at the "I" position is 2, it means that it must be the first person at the position of Pos = 2. It also means that there must be two people in front of him. Then we can set aside two vacancies, let the first two people insert and use the line segment tree. The interval storage can store the number of people and determine recursion based on the number of people and Pos location, note that no matter how many people have been inserted in front, the position "I" POS is how much, it is necessary to set aside the position of the pos-1, because he must be the first person behind the pos-1 individual.

 

Code:

 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <string> 8 #include <bitset> 9 #include <vector>10 #include <queue>11 #include <stack>12 #include <cmath>13 #include <list>14 //#include <map>15 #include <set>16 using namespace std;17 /***************************************/18 #define ll long long19 #define int64 __int6420 #define PI 3.141592721 /***************************************/22 const int INF = 0x7f7f7f7f;23 const double eps = 1e-8;24 const double PIE=acos(-1.0);25 const int d1x[]= {0,-1,0,1};26 const int d1y[]= {-1,0,1,0};27 const int d2x[]= {0,-1,0,1};28 const int d2y[]= {1,0,-1,0};29 const int fx[]= {-1,-1,-1,0,0,1,1,1};30 const int fy[]= {-1,0,1,-1,1,-1,0,1};31 const int dirx[]= {-1,1,-2,2,-2,2,-1,1};32 const int diry[]= {-2,-2,-1,-1,1,1,2,2};33 /*vector <int>map[N];map[a].push_back(b);int len=map[v].size();*/34 /***************************************/35 void openfile()36 {37     freopen("data.in","rb",stdin);38     freopen("data.out","wb",stdout);39 }40 priority_queue<int> qi1;41 priority_queue<int, vector<int>, greater<int> >qi2;42 /**********************华丽丽的分割线,以上为模板部分*****************/43 const int M= 200100;44 struct tree45 {46     int Nper;47     int left,right;48 } node[M*4];49 int p[M][2];50 int num[M];51 void build__tree(int id,int l,int r)52 {53     node[id].left=l;54     node[id].right=r;55     int mid=(l+r)/2;56     if (l==r)57     {58         node[id].Nper=1;59         return ;60     }61     build__tree(id*2,l,mid);62     build__tree(id*2+1,mid+1,r);63     node[id].Nper=node[id*2].Nper+node[id*2+1].Nper;64 }65 void updata(int id,int pos,int val)66 {67     if (node[id].left==node[id].right)68     {69         num[node[id].left]=val;70         node[id].Nper--;71         return ;72     }73     if (pos<node[id*2].Nper)74         updata(id*2,pos,val);75     else76         updata(id*2+1,pos-node[id*2].Nper,val);77     node[id].Nper--;78 }79 int main()80 {81     int n;82     while(scanf("%d",&n)!=EOF)83     {84         int i,j;85         build__tree(1,1,n);86         for(i=0;i<n;i++)87             scanf("%d%d",&p[i][0],&p[i][1]);88         for(i=n-1;i>=0;i--)89         {90             updata(1,p[i][0],p[i][1]);91         }92         printf("%d",num[1]);93         for(i=2;i<=n;i++)94             printf(" %d",num[i]);95         printf("\n");96     }97     return 0;98 }
View code

 

Poj2828 (buy tickets)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.