Portal: ZOJ 3635
Cinema in Akiba Time limit: 3 Seconds Memory Limit: 65536 KB
Cinema in Akiba (CIA) was a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of theCIA is very interesting, as there are only one row so that every audience can enjoy the wonderful MOV IES without any annoyance by and audiences sitting in front of him/her.
The ticket for CIA is strange, too. There is n seats in CIA and they is numbered from 1 to in n order. Apparently, n tickets'll be sold everyday. When buying a ticket, if there is k tickets left, your ticket number would be an integer i (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 the ith geek is ai . Can them find out their seat numbers?
Input
The input contains multiple test cases. Process to end of file.
The first line of all case was an integer n (1≤ n ≤50000), the number of geeks as well as the number of seats in CIA. Then follows a line containing n integers a1 , a2 , ..., an (1≤ ai ≤ n - i + 1), as described above. The third line m is a integer (1≤ m ≤3000), the number of queries, and the next line is m 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. The integer is the seat number of the ith qith 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
Idea: Linked list simulates a row of seat 1-n, constantly deleted, and then record the seat number. Using the normal list of lists, it is necessary to tle,so on the block list to compensate for the disadvantage that the index of the list is too slow.
Code:
#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[tree-shaped array]