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 ≤ 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 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
题目链接
前言:写的都是泪啊,好久没写过树状数组了,算是温习一下吧,写了一下午,又加了个晚上,还好搞定了,要不觉都睡不着了;哈哈
讲解:本题用到树状数组来统计,当前空位的个数,来判断是否能够安排在此位置;用二分法,优化,防止超时;代码中详解;
#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 ;
}