zoj 3635 Cinema in Akiba (树状数组求第K大)
2015-10-22 10:54
190 查看
Cinema in Akiba
Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA 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 integer i (1 ≤ i ≤ k), and you should choose theith 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 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 in CIA. Then follows a line containing n integers a1, a2, ..., an (1 ≤ ai ≤ n - i + 1), as described above. The third line is an integer m (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 ith integer is the seat number of the qith geek.
Sample Input
Sample Output
View Code
Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA 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 integer i (1 ≤ i ≤ k), and you should choose theith 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 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 in CIA. Then follows a line containing n integers a1, a2, ..., an (1 ≤ ai ≤ n - i + 1), as described above. The third line is an integer m (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 ith integer is the seat number of the qith geek.
Sample Input
3 1 1 1 3 1 2 3 5 2 3 3 2 1 5 2 3 4 5 1
Sample Output
1 2 3 4 5 3 1 2 思路比较清晰。 树状数组+二分 具体见注释
/************************************************************************* > File Name: code/zoj/3635.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年10月22日 星期四 10时03分36秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) using namespace std; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; const int N=5E5+7; int t ,a ; int ans ,k; int n,m; int lowbit( int x) { return x&(-x); } void update ( int x,int delta) { for ( int i = x ; i <= n ; i = i + lowbit(i)) { t[i] = t[i] + delta; } } int sum( int x) { int res = 0 ; for ( int i = x; i >= 1 ; i = i - lowbit(i)) { res = res + t[i]; } return res; } int bin_search(int l,int r) { while (l<r) { int mid = (l+r)>>1; if (sum(mid)<k) l = mid + 1; else r = mid ; } return r; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif while (scanf("%d",&n)!=EOF) { ms(t,0); ms(ans,0); ms(a,0); for ( int i = 1 ; i <= n ; i++) update(i,1); //1表示没有被占,初始所有位置都没有被占。 for ( int i = 1 ; i <= n ; i++) { scanf("%d",&k); //每次要占第k个没有被占的位置,由于被占的位置被设置成0,所以就是找第k大的。 int posi = bin_search(1,n); ans[i] =posi; update(posi,-1); //将被占的位置设置成0 } scanf("%d",&m); int x; for ( int i = 1 ; i < m ; i++) { scanf("%d",&x); printf("%d ",ans[x]); } scanf("%d",&x); printf("%d\n",ans[x]); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐
- JS实现新浪博客左侧的Blog管理菜单效果代码
- 5.5 GDI 映射模式
- 按钮写到UItableView的头视图还是列表之外
- TableLayout中TextView文本无法自动换行
- [Nginx]ngx_http_top_header_filter地址为0
- 【codevs 2975】与2,3,5无关的数
- iOS开发 实战篇---Xcode7免费真机测试流程
- C/C++语言编码规范
- hadoop编译的本地库会依赖操作系统吗?比如amd64的系统编译的可以在其他X86OS上工作吗??hadoop本地库与系统版本不一致
- LoadRunner之自定义HTTP请求
- MySQL创建触发器
- 判断某个字符传中是否包含另一个字符!
- Event原理解析
- 高端唯有定制,把 sublime 打造成专属的 IDE
- 书籍分享
- Queue 与List, LinkedList与 ArrayList 用法与区别
- 常成员变量数组初始化的问题
- WIFI 芯片相关
- 编辑删除收藏在tableView的内容
- 【培训】MySQL