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.访问 ...
随机推荐
- Django的restful api三方包:djangorestframework
文档位置:https://www.django-rest-framework.org/ 代码位置:https://github.com/encode/django-rest-framework/tre ...
- #include <NOIP2009 Junior> 细胞分裂 ——using namespace wxl;
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...
- 要做的题目-要用到hadoop资源
关于项目,我出两个练手题目: 一.多机数据处理.有 10 台机器,每台机器上保存着 10 亿个 64-bit 整数(不一定刚好 10 亿个,可能有上下几千万的浮动),一共约 100 亿个整数(其实一共 ...
- jquery easyui里datagrid用法记录
1.删除行方法(deleteRow) $(); //1代表选中的行索引 2.删除多行数据 var rows = $('#ruleManagementTable').datagrid("get ...
- 高性能WEB开发:重排与重绘
DOM编程可能最耗时的地方,重排和重绘. 1.什么是重排和重绘 浏览器下载完页面中的所有组件——HTML标记.JavaScript.CSS.图片之后会解析生成两个内部数据结构——DOM树和渲染树. D ...
- CentOS6.4下Samba服务器的安装与配置
一.先恶狠狠地吐槽一下: 这篇随笔真是让我折腾了2天2夜才敢下笔写!!!为什么呢?之前是通过去Samba的官网下载的源码包,也就是.tar.gz来进行安装配置,不过这个让我折腾来折腾去就是没折腾出结果 ...
- C++中++i与i++效率比较
解析: 在这里声明,简单的比较前缀自增运算符和后缀自增运算符的效率是片面的,因为存在很多因素影响这个问题的答案.首先考虑内建数据类型的情况:如果自增运算表达式的结果没有被使用,而是仅仅简单的用于增加一 ...
- python爬虫模拟登陆
python爬虫模拟登陆 学习了:https://www.cnblogs.com/chenxiaohan/p/7654667.html 用的这个 学习了:https://www.cnblogs.co ...
- [React] Safely setState on a Mounted React Component through the useEffect Hook
In the class version of this component, we had a method called safeSetState which would check whethe ...
- Java最大的优势真的在于跨平台吗?
下面讨论仅仅针对PC端和移动端. 曾经是,但如今已经不是了. 有跨平台需求的仅仅是client应用.而不是服务端.比如桌面应用,你的客户可能是Windows用户.也可能是Linux用户,这时候假设不想 ...