Buy Tickets(线段树单点更新)
来源:互联网 发布:电脑抓阄软件 编辑:程序博客网 时间:2024/05/19 17:25
Buy Tickets
Time Limit : 8000/4000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 43 Accepted Submission(s) : 21
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.
40 771 511 332 6940 205231 192431 38900 31492
77 33 69 5131492 20523 3890 19243
题意:给一个整数n接下来n行每行两个数a,b表示b这个数字插在第a个数字后边,问最后形成的序列?
题解:此题建树的时候需要逆序输入数据,这样将后续的人员在对应位置向后插入即可,运用线段树来处理和操作
代码:
#include<cstdio>#include<cstring>#include<algorithm>#define maxn 200005using namespace std;int sum[maxn*4]; //注意此处定义是最大值的4倍int ans[maxn];void pushup(int pos) //求左右子树和{ sum[pos]=sum[pos*2]+sum[pos*2+1];}void buildtree(int pos,int l,int r) //建树{ if(l==r) { sum[pos]=1;//起始时刻,让所有的位置都站上人 return ; } int mid=(l+r)/2; buildtree(pos*2,l,mid); buildtree(pos*2+1,mid+1,r); pushup(pos);//建成的树最底端每个位置都是第一个越往根部,人的编号越大,根处编号为n} void update(int o,int l,int r,int pos,int v){ if(l==r) { sum[o]=0;//当位于这个位置的时候 ans[r]=v; return ;//结束 } int mid=(l+r)/2; if(pos<=sum[o*2]) update(o*2,l,mid,pos,v);//搜索左子树时 else update(o*2+1,mid+1,r,pos-sum[o*2],v);//搜索右子树因为右子树左端的值是左子树右端值+1所以查找右子树位置时查找到pos-左子树长度就可以了 pushup(o); } int main(){ int n,m,j,i; int a[maxn],b[maxn]; while(scanf("%d",&n)!=EOF) { buildtree(1,1,n); for(i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]); for(i=n;i>0;i--) //注意是逆序 update(1,1,n,a[i]+1,b[i]);//+1是因为题目数据是从0开始的 我们输数据的时候是从第一位而不是第0位输的 所以要+1,让位置对应 for(i=1;i<=n;i++) printf("%d ",ans[i]); printf("\n"); } return 0;}
- Buy Tickets(线段树单点更新)
- poj2828 Buy Tickets 线段树,单点更新
- Buy Tickets ---- 线段树单点更新
- poj2828 Buy Tickets 单点更新线段树
- POJ2828 Buy Tickets 线段树单点更新
- poj2828 Buy Tickets(线段树,单点更新)
- Poj2828 Buy Tickets 线段树、单点更新
- POJ2828 Buy Tickets(线段树,单点更新)
- 线段树单点更新 poj2828 buy tickets
- Buy Tickets (线段树)单点更新
- poj 2828 Buy Tickets(线段树单点更新)
- poj 2828 Buy Tickets (线段树---单点更新)
- poj2828 buy tickets(线段树单点更新)
- POJ - 2828 Buy Tickets(线段树单点更新)
- POJ 题目2828 Buy Tickets(线段树单点更新)
- POJ2828--Buy Tickets(线段树,单点更新)
- POJ 2828 Buy Tickets(线段树--单点更新)
- POJ 2828 Buy Tickets (线段树:单点更新)
- Python学习笔记·交互式图形编程
- Ural 1082|Gaby Ivanushka|数学推导|快速排序|程序分析
- HDU 5618 CDQ分治(三维偏序)
- QtE与Qtopia的概念
- tensorflow中padding方式理解:same与valide
- Buy Tickets(线段树单点更新)
- Android中dp,px,sp概念梳理以及如何做到屏幕适配
- 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除。
- 1-前端之路的开始——个小动画
- (M)Dynamic Programming:688. Knight Probability in Chessboard
- PCB布线FORTRAN程序例子1
- 批量将图像原地(不开辟新空间)顺时旋转90度保存以及灰度化
- 架构师日记——MongoDB简介和安装使用
- 002_LeetCode_2 Add Two Numbers 题解