Cinema in Akiba


Time Limit: 3 Seconds      Memory Limit: 65536 KB

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 ≤ ik), 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 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 ≤ ain - 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 ≤ qin), 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
题目链接

前言:写的都是泪啊,好久没写过树状数组了,算是温习一下吧,写了一下午,又加了个晚上,还好搞定了,要不觉都睡不着了;哈哈

讲解:本题用到树状数组来统计,当前空位的个数,来判断是否能够安排在此位置;用二分法,优化,防止超时;代码中详解;

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
#define NN 50050
int n;
int a[NN],ans[NN];
int vis[NN];
int lowbit(int k)
{
return k&(-k);
}
void begin()
{
int i,t,j;
for(i=n;i>=;i--)
{
t=;
for(j=i-lowbit(i)+;j<=i;j++)
t=a[j]+t;
a[i]=t;
}
}
int query(int x)//sum 就是代表 前 x 个座位有几个是空的;
{
int sum=;
while(x>)
{
sum=sum+a[x];
x=x-lowbit(x);
}
return sum;
}
int find(int num,int x,int y)//第一次提交没有二分,直接超时了,
{
int mid=(x+y)/; // vis[2]=1;
int yy=query(mid); //例如 1 1 1 2 第二号已经安排人了,但是前两个的和,也为1,所以不符合,继续向前递归;
if(num==yy && vis[mid]==) // 如果当前的空位数,正好等于需要安排的位置,并且没有被安排其他人;
return mid; //直接返回位置;
if(num>yy)
find(num,mid+,n);
else find(num,x,mid);
}
void add(int y,int x)
{
x=find(x,x,n);
ans[y]=x; //把第 y 号人,安排到第 X 位上;
vis[x]=; //已经安排过位置的座位标记为 1 ,下次查找直接忽略;
while(x<=n)
{
a[x]=a[x]-;
x=x+lowbit(x);
}
}
int main()
{
int i,j,k,t,m,x;
while(cin>>n)
{
fill(a,a+n+,);
memset(ans,,sizeof(ans));
memset(vis,,sizeof(vis));
begin();
for(i=;i<=n;i++)
{
cin>>x;
add(i,x); // 插入数时,直接计算位置;
}
cin>>m;
for(j=; j<=m; j++)
{
cin>>x;
if(j==) cout<<ans[x];//注意格式啊,第二次提交格式错了一次,呜呜
else cout<<" "<<ans[x];
}
cout<<endl;
}
return ;
}

2015—04-30更新代码:

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
#define NN 50050
int n;
int a[NN],ans[NN];
int lowbit(int k)
{
return k&(-k);
}
void begin()
{
for(int i=;i<=n;i++)
{
a[i]= lowbit(i);
}
}
int query(int x)
{
int sum=;
while(x>)
{
sum=sum+a[x];
x=x-lowbit(x);
}
return sum;
}
void update(int x)
{
while(x<=n)
{
a[x]=a[x]-;
x=x+lowbit(x);
}
}
void add(int y,int x)
{
int l = x,r = n,mid;
while(l<r)
{
mid = (l+r)/;
if(query(mid)>=x) r = mid;
else l = mid+;
}
update(l);
ans[y] = l;
}
int main()
{
int i,j,k,t,m,x;
while(cin>>n)
{
begin();
for(i=;i<=n;i++)
{
cin>>x;
add(i,x);
}
cin>>m;
for(j=; j<=m; j++)
{
cin>>x;
if(j==) cout<<ans[x];//注意格式啊,第二次提交格式错了一次,呜呜
else cout<<" "<<ans[x];
}
cout<<endl;
}
return ;
}

ZOJ 3635 Cinema in Akiba (第一次组队) 树状数组+二分的更多相关文章

  1. ZOJ 3635 Cinema in Akiba(线段树)

    Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is ful ...

  2. ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]

    门户:problemCode=3635">ZOJ 3635 Cinema in Akiba Time Limit: 3 Seconds      Memory Limit: 65536 ...

  3. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  4. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  5. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  6. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  7. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  8. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

  9. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

随机推荐

  1. 深度学习教程Deep Learning Tutorials

    Deep Learning Tutorials Deep Learning is a new area of Machine Learning research, which has been int ...

  2. 数学图形(2.18)Hyperbolical conical spiral双曲圆锥螺线

    双曲圆锥螺线 #http://www.mathcurve.com/courbes3d/spiralehyperbolique/spiralehyperbolique.shtml vertices = ...

  3. Orchard运用 - 定制呈现最新博客文章

    每个博客系统为了吸引更多访问量,一般都会在首页或侧边栏列举一些最新文章/随笔以获取更多点击.其实也就是查询出最新的几篇文章并按照简练的方式呈现,比如一般都只有标题及其对应的链接,有时也会标注一下作者和 ...

  4. 复习原生ajax

    function ajax(url, fnSucc, fnFaild) { //1.创建 if(window.XMLHttpRequest) { var oAjax=new XMLHttpReques ...

  5. jquery ajax 获取 json 文件数据

    [ {"name":"project1"}, {"name":"project2"}, {"name" ...

  6. 《java 语言程序设计》第1章编程练习

    1.1 public class test { public static void main(String[] args) { System.out.println("Welcome to ...

  7. HDFS的常用操作

    本文地址:http://www.cnblogs.com/archimedes/p/hdfs-operations.html,转载请注明源地址. 1.HDFS文件的权限以及读写操作 HDFS文件的权限: ...

  8. Android -- 压缩与解压文件

    我在做一个项目中,工程文件中有一个功能需要很多图片,图片与app一起打包下来的话有30+M,那么我们就考虑另外下载压缩包,我们将图片取出,工程就只有4+M了,哈哈哈哈,呵呵,真恐怖.那么这样就涉及到另 ...

  9. JSP2.0自定义标签

    JSP1.0中可以通过继承TagSupport或者BodyTagSupport来实现自定义的tag处理方法. JSP2.0中也支持另外一种更为简单的自定tag的方法,那就是直接讲JSP代码保存成*.t ...

  10. Android Studio中mac上面的安装

    Android Studio中mac上面的安装 学习了:https://blog.csdn.net/xianrenli38/article/details/79347170 http://www.an ...