ZOJ 3635 Cinema in Akiba[ 块状数组 ]
来源:互联网 发布:浙江省定额计价软件 编辑:程序博客网 时间:2024/05/18 02:04
传送门:ZOJ 3635
Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout ofCIA is very interesting, as there is only one row so that every audience can enjoy the wonderful movies without any annoyance by other audiences sitting in front of him/her.
The ticket for CIA is strange, too. There are n seats in CIA and they are numbered from 1 to n in order. Apparently, n tickets will be sold everyday. When buying a ticket, if there are k tickets left, your ticket number will be an integeri (1 ≤i ≤ k), and you should choose the ith empty seat (not occupied by others) and sit down for the film.
On November, 11th, n geeks go to CIA to celebrate their anual festival. The ticket number of theith geek isai. Can you help them find out their seat numbers?
Input
The input contains multiple test cases. Process to end of file.
The first line of each case is an integer n (1 ≤ n ≤ 50000), the number of geeks as well as the number of seats inCIA. Then follows a line containingn integers a1,a2, ..., an (1 ≤ ai ≤n - i + 1), as described above. The third line is an integerm (1 ≤m ≤ 3000), the number of queries, and the next line ism integers,q1, q2, ..., qm (1 ≤qi ≤ n), each represents the geek's number and you should help him find his seat.
Output
For each test case, print m integers in a line, seperated by one space. Theith integer is the seat number of theqith geek.
Sample Input
31 1 131 2 352 3 3 2 152 3 4 5 1
Sample Output
1 2 34 5 3 1 2
思路:链表模拟一排座位1-n,不断删除,然后记录座位号。用普通的链表模拟,必然TLE,so上块状链表,来弥补链表索引太慢的缺点。
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<vector>#include<algorithm>#include<climits>#define INF INT_MAXusing namespace std;struct node{ int num[1000]; int sz; node* next; node(int sz){ this->sz=sz; next=0; }}*head=NULL;int local[50010];node *get_pos(int &x){ node *t=head->next; while(t->next&&x>t->sz) { x-=t->sz; t=t->next; } return t;}void del(node *pos,int x,int ii){ local[ii]=(pos->num)[x-1]; for(int i=x;i<pos->sz;i++) (pos->num)[i-1]=(pos->num)[i]; (pos->sz)--;}void build_bl(int n){ node *ret=head; int sz=ceil(sqrt(n)); int cur=0; for(int i=0;i<n/sz;i++) { node *t=new node(sz); for(int j=0;j<sz;j++) (t->num)[j]=(++cur); ret->next=t; ret=t; } if(n%sz) { node *t=new node(n%sz); for(int i=0;i<n%sz;i++) (t->num)[i]=(++cur); ret->next=t; }}void free_head(){ node *p=head; while(head->next) { head=head->next; free(p); p=head; }}int main(){ int n,m; while(scanf("%d",&n)!=EOF) { head=new node(0); build_bl(n); for(int i=1;i<=n;i++) { //printf("__hear\n"); int x; scanf("%d",&x); node *pos=get_pos(x); del(pos,x,i); } free_head(); scanf("%d",&m); int q; scanf("%d",&q); printf("%d",local[q]); for(int i=1;i<m;i++) { scanf("%d",&q); printf(" %d",local[q]); } printf("\n"); } return 0;}
- ZOJ 3635 Cinema in Akiba[ 块状数组 ]
- ZOJ-3635-Cinema in Akiba
- ZOJ 3635 Cinema in Akiba
- zoj 3635 Cinema in Akiba
- ZOJ 3635 Cinema in Akiba(树状数组 + 二分)
- ZOJ 3635 Cinema in Akiba (树状数组+二分)
- zoj 3635 Cinema in Akiba(树状数组+二分)
- ZOJ 3635 Cinema in Akiba(二分+树状数组)
- zoj 3635 Cinema in Akiba 二分+树状数组
- ZOJ 3635 Cinema in Akiba【线段树】
- ZOJ 3635——Cinema in Akiba(树状数组+二分)
- zoj 3635 Cinema in Akiba 树状数组求第K大
- Poj 2828 Buy Tickets \ Zoj 3635 Cinema in Akiba
- ZOJ 3635 Cinema in Akiba(线段树)
- ZOJ 题目3635 Cinema in Akiba(线段树插空)
- zoj3635-Cinema in Akiba 树状数组+二分
- ZOJ Monthly, August 2012 - C Cinema in Akiba 树状数组+二分
- ZOJ3635 Cinema in Akiba
- 鼠标响应事件 当鼠标经过/移出时切换css
- wincc项目归档数据导出
- 程序员生存定律--成长路上常见的坑(2)
- Linux中fork()函数讲解(转载)
- How To Fix “W: Failed to fetch gzip:/var/lib/apt/lists/partial/… Hash Sum mismatch” Error
- ZOJ 3635 Cinema in Akiba[ 块状数组 ]
- Eclipse下使用MinGW中的gdb调试OpenCV程序
- android 事件传递
- Cocoa 框架 For iOS(二)对象的分配初始化、内省、单例
- SQL SERVER 服务启动失败
- UVA - 120 Stacks of Flapjacks
- HDU 1533 Going Home(KM完美匹配)
- Java反射API与动态代理
- OC1_类和对象