ZOJ 3635 Cinema in Akiba (第一次组队) 树状数组+二分
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 ;
}
ZOJ 3635 Cinema in Akiba (第一次组队) 树状数组+二分的更多相关文章
- ZOJ 3635 Cinema in Akiba(线段树)
Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is ful ...
- ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]
门户:problemCode=3635">ZOJ 3635 Cinema in Akiba Time Limit: 3 Seconds Memory Limit: 65536 ...
- TZOJ 4602 高桥和低桥(二分或树状数组+二分)
描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- 树状数组+二分||线段树 HDOJ 5493 Queue
题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- The Stream of Corning 2( 权值线段树/(树状数组+二分) )
题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
随机推荐
- Linux内核的idle进程分析
1. idle是什么 简单的说idle是一个进程,其pid号为 0.其前身是系统创建的第一个进程.也是唯一一个没有通过fork()产生的进程. 在smp系统中,每一个处理器单元有独立的一个执行队列,而 ...
- 程序编译是出现"field has incomplete type"问题的解决
在编译程序是出现了如下错误, 类或结构体的前向声明只能用来定义指针对象或引用,因为编译到这里时还没有发现定义,不知道该类或者结构的内部成员,没有办法具体的构造一个对象,所以会报错. 将类成员改成指针就 ...
- 附1 consul常用命令+常用选项
之后每用到一个command或options,都会记录在这里. 常用命令command: agent 作用:运行一个consul agent join 作用:将agent加入到consul clust ...
- sscanf %*s
一次在源码里看到 %*s 的格式,从未见过百思不得其解,今天用google的code搜索,搜到一些使用范例,猜测%*s 是说这里有一些字符,长度不一定,按正则表达式的习惯,*代办任意非负整数.例如: ...
- [C#.NET] X509 數位電子簽章
摘自: http://www.dotblogs.com.tw/yc421206/archive/2012/06/30/73140.aspx 在上篇[C#.NET] 字串及檔案,利用 RSA 演算法加解 ...
- sh: 1: node: Permission denied
ionic app 开发的时候,https://dashboard.ionicframework.com/welcome ionic start myApp tabs 报错了https://www.j ...
- [Functional Programming] Use Task/Async for Asynchronous Actions
We refactor a standard node callback style workflow into a composed task-based workflow. Original Co ...
- 使用HTML5的两个api,前端js完成图片压缩
主要用了两个html5的 API,一个file,一个canvas,压缩主要使用cnavas做的,file是读取文件,之后把压缩好的照片放入内存,最后内存转入表单下img.src,随着表单提交. 照片是 ...
- Drag & drop a button widget
In the following example, we will demonstrate how to drag & drop a button widget. #!/usr/bin/pyt ...
- 重置 radio 和 checkbox 的样式
代码: <!doctype html> <html> <head> <meta charset="utf-8"> <title ...