码迷,mamicode.com
首页 > 其他好文 > 详细

poj2828(Buy Tickets)

时间:2014-10-28 11:43:29      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   for   sp   

题目地址:Buy Tickets

 

题目大意:

    模拟一个插队买票的情况,有N个人,每个人都有一个pos要插到第几个人的后面,还有一个val值,更新N次插队的顺序,插入完毕最后按照队伍顺序输出val值。

 

解题思路:

    直接模拟插入是不行的,会TLE,如果正着插入的话,会导致下一个人要排在前面的话,插入的pos位置后面的都要往后移一位,时间复杂度太大,这时候呢要想如果从后面依次往前插入会不会好点操作减省时间。从后面插入此人的pos一定是他想得到的位置,而且是确定的,假如最后一人pos位置是0,说明他一定要插在最前面,假如一个“i”位置的pos是2说明他一定是在pos=2这个位置最前面的一个人,也说明他前面一定两个人,然后我们可以留出两个空位,让他前面的两个人插入, 用线段树实现,区间存可以存储人的数量,根据人的数量和pos的位置去判断递归,需要注意无论前面已经插入了多少人,位置“i”的pos是多少,就要留出pos-1的位置,因为他一定是在pos-1个人后面的第一个人。

 

代码:

bubuko.com,布布扣
 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 long
19 #define int64 __int64
20 #define PI 3.1415927
21 /***************************************/
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 tree
45 {
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     else
76         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)

标签:style   blog   http   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/ZhaoPengkinghold/p/4056269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!