【poj 2828】 Buy Tickets(线段树/树状数组)
来源:互联网 发布:网络大电影审批流程 编辑:程序博客网 时间:2024/04/29 16:31
【poj 2828】 Buy Tickets(线段树/树状数组)
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
40 771 511 332 6940 205231 192431 38900 31492
Sample Output
77 33 69 5131492 20523 3890 19243
Hint
The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.
Source
按顺序把n个人插入队列 给出每个人插入时的位置 正着不好想 可以倒着做 从第n到第1个人遍历
对于第i个人的位置id 其实就是找当前队列第id个空位置
例如第一组样例
40 771 511 332 69
给出下标从0开始 不好做 所以把下标统一后移一位
69->333->2
当51->2时 位置1可用 位置2.3被分配了 所以给51位置4
77->1
用线段树很容易写 找到从头开始的第k个空位置
代码如下:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int bit[800800];int ans[200200];//初始化n个空位void init(int site,int l,int r){ if(l == r) { bit[site] = 1; return; } int mid = (l+r)>>1; init(site<<1,l,mid); init(site<<1|1,mid+1,r); bit[site] = bit[site<<1]+bit[site<<1|1];}//找第data个空位int Search(int site,int l,int r,int data){// printf("%d %d %d\n",l,r,data); //第data个空位肯定在当前遍历的子树中 剩余空位-- bit[site]--; //l == r说明遍历到叶子 也就是找到了第data个空位 if(l == r) return l; int mid = (l+r)>>1; //左树的位置不够 在右树中找第(data-(左树空位置))个空位置 if(bit[site<<1] < data) { return Search(site<<1|1,mid+1,r,data-bit[site<<1]); } //否则只直接在左树找第data个空位置 else { return Search(site<<1,l,mid,data); }}pair <int,int> p[200200];int main(){ int n; while(~scanf("%d",&n)) { init(1,1,n); for(int i = 0; i < n; ++i) { scanf("%d%d",&p[i].first,&p[i].second); } for(int i = n-1; i >= 0; --i) {// int tmp = Search(1,1,n,p[i].first+1);// puts("-------");// printf("%d\n",tmp); ans[Search(1,1,n,p[i].first+1)] = p[i].second; } for(int i = 1; i <= n; ++i) { if(i != 1) putchar(' '); printf("%d",ans[i]); } puts(""); } return 0;}
树状数组两种做法 不过核心都跟线段树一样 找第k个空位
第一种就是二分 找第一个id 满足 前id项和==k
跟线段树的思想类似
直接上代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int bit[200200];int n;int Lowbit(int x){ return x&(-x);}void Add(int x,int data){ while(x <= n) { bit[x] += data; x += Lowbit(x); }}int Sum(int x){ int ans = 0; while(x > 0) { ans += bit[x]; x -= Lowbit(x); } return ans;}pair <int,int> p[200200];int ans[200200];int main(){ int l,r,c; while(~scanf("%d",&n)) { for(int i = 0; i < n; ++i) { scanf("%d%d",&p[i].first,&p[i].second); p[i].first++; } for(int i = 1; i <= n; ++i) Add(i,1); for(int i = n-1; i >= 0; --i) { l = 1, r = n; c = -1; while(l <= r) { int mid = (l+r)>>1; int tmp = Sum(mid); if(tmp >= p[i].first) { if(tmp == p[i].first) c = mid; r = mid-1; } else l = mid+1; } ans[c] = p[i].second; Add(c,-1); } for(int i = 1; i <= n; ++i) { if(i != 1) putchar(' '); printf("%d",ans[i]); } puts(""); } return 0;}
第二种方法比较优美 按位找 主要是findk函数,节省了很大一段时间
代码如下:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int bit[200200];int n;int Lowbit(int x){ return x&(-x);}void Add(int x,int data){ while(x <= n) { bit[x] += data; x += Lowbit(x); }}int Sum(int x){ int ans = 0; while(x > 0) { ans += bit[x]; x -= Lowbit(x); } return ans;}int findk(int data){ int id = 0; for(int i = 17; i >= 0; --i) { id ^= 1<<i; if(id <= n && data > bit[id]) data -= bit[id]; else id ^= 1<<i; } return id+1;}pair <int,int> p[200200];int ans[200200];int main(){ int l,r,c; while(~scanf("%d",&n)) { for(int i = 0; i < n; ++i) { scanf("%d%d",&p[i].first,&p[i].second); p[i].first++; } for(int i = 1; i <= n; ++i) Add(i,1); for(int i = n-1; i >= 0; --i) { c = findk(p[i].first); ans[c] = p[i].second; Add(c,-1); } for(int i = 1; i <= n; ++i) { if(i != 1) putchar(' '); printf("%d",ans[i]); } puts(""); } return 0;}
- poj 2828 -- Buy Tickets ( 线段树 / 树状数组 )
- 【poj 2828】 Buy Tickets(线段树/树状数组)
- POJ 2828 Buy Tickets (树状数组 or 线段树)
- poj 2828 Buy Tickets(树状数组 | 线段树)
- POJ 2828 Buy Tickets(树状数组)
- POJ 2828 Buy Tickets(树状数组)
- POJ 2828 Buy Tickets(神题!线段树or树状数组)
- POJ 2828——Buy Tickets(树状数组,线段树——逆序遍历)
- poj 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets (线段树)
- POJ 2828--Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- XSS初探
- NSURLSessionDownloadTask 出现 NSURLErrorDomain的解决方案
- 来玩Play框架01 简介
- 【Smarty】利用Smarty编写一个VC分离的php网站
- oracle 执行计划 方法汇总
- 【poj 2828】 Buy Tickets(线段树/树状数组)
- 权限中的【追加】与【追加到】的区分
- [leetcode] 237. Delete Node in a Linked List
- HTML DOM - 元素
- android Pbap下载手机电话簿协议介绍
- Jquery+json
- Restful API的设计与实践
- 瞭解Unicode
- Android之使用HttpPost提交数据到服务器(Android手机客户端和后台服务器交互)