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.访问 ...
随机推荐
- go语言基础之递归函数的调用流程
一.递归函数的调用流程 package main //必须 import "fmt" func test(a int) { if a == 1 { //函数终止调用的条件,非常重要 ...
- 使用CadLib实现CAD(dxf、dwg格式)文件的读取和显示 【转】
参考文章:CadLib 3.5 documentationhttps://www.woutware.com/doc/cadlib3.5/Index.aspx 读取:定义DxfModel类型的变量mod ...
- 用 Git Hooks 进行自动部署
原文发表于 http://ourai.ws/posts/deployment-with-git-hooks/ 昨天开始接手开发公司前端团队的主页,在稍微修改点东西后推送到远程仓库想看下线上结果时发现并 ...
- ownCloud 的六大神奇用法
ownCloud 是一个自行托管的开源文件同步和共享服务器.就像“行业老大” Dropbox.Google Drive.Box 和其他的同类服务一样,ownCloud 也可以让你访问自己的文件.日历. ...
- T-SQL 之 触发器
触发器可以做很多事情,但也会带来很多问题.正确的使用在于在适当的时候使用,而不要在不适当的时候使用它们. 触发器的一些常见用途如下: [1] 弹性参照完整性:实现很多DRI不能实现的操作(例如,跨数据 ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- android安装应用程序工具类
/** * 安装APK文件 *@param APK文件 *Version: *author:YangQuanqing */ private void installAPK(File file){ // ...
- 解析_theme_build_registry()和_theme_process_registry()
Drupal使用_theme_build_registry()和_theme_process_registry()两个函数构建theme registry.theme registry是theme h ...
- HDU 5005(Compromise-双人目标为最大化不同值的博弈)
Compromise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- Android OpenGL 开发
2013-06-30 Android OpenGL 开发 Android提供OpenGL包,专门用于3D的加速和渲染等. OpenGL, Open Graphics Library, 是一个专业的图形 ...