ZOJ 3635 Cinema in Akiba (第一次组队) 树状数组+二分

本文介绍了一个使用树状数组结合二分法解决特定电影院座位分配问题的方法。该问题涉及根据输入的购票顺序和数量,计算每个顾客最终获得的座位编号。文章详细解释了解决方案的实现细节,并提供了两段完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题目链接

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

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<queue>
 9 using namespace std;
10 #define NN 50050
11 int n;
12 int a[NN],ans[NN];
13 int vis[NN];
14 int lowbit(int k)
15 {
16     return k&(-k);
17 }
18 void begin()             
19 {
20     int i,t,j;
21     for(i=n;i>=1;i--)
22     {
23        t=0;
24        for(j=i-lowbit(i)+1;j<=i;j++)
25              t=a[j]+t;
26         a[i]=t;
27     }
28 }
29 int query(int x)//sum  就是代表  前 x 个座位有几个是空的;
30 {
31     int sum=0;
32     while(x>0)
33     {
34         sum=sum+a[x];
35         x=x-lowbit(x);
36     }
37     return sum;
38 }
39 int find(int num,int x,int y)//第一次提交没有二分,直接超时了,
40 {
41     int mid=(x+y)/2;           //                vis[2]=1;
42     int yy=query(mid);        //例如 1 1 1 2    第二号已经安排人了,但是前两个的和,也为1,所以不符合,继续向前递归;
43     if(num==yy && vis[mid]==0) // 如果当前的空位数,正好等于需要安排的位置,并且没有被安排其他人;
44         return mid;            //直接返回位置;
45     if(num>yy)
46         find(num,mid+1,n);
47     else find(num,x,mid);
48 }
49 void add(int y,int x)
50 {
51      x=find(x,x,n);
52        ans[y]=x;   //把第 y 号人,安排到第 X 位上;
53        vis[x]=1;   //已经安排过位置的座位标记为 1 ,下次查找直接忽略;
54     while(x<=n)
55     {
56         a[x]=a[x]-1;
57         x=x+lowbit(x);
58     }
59 }
60 int main()
61 {
62     int i,j,k,t,m,x;
63     while(cin>>n)
64     {
65         fill(a,a+n+1,1);
66         memset(ans,0,sizeof(ans));
67         memset(vis,0,sizeof(vis));
68         begin();
69         for(i=1;i<=n;i++)
70         {
71             cin>>x;
72             add(i,x);           //  插入数时,直接计算位置;
73         }
74         cin>>m;
75         for(j=1; j<=m; j++)
76         {
77            cin>>x;
78             if(j==1) cout<<ans[x];//注意格式啊,第二次提交格式错了一次,呜呜
79            else cout<<" "<<ans[x];
80         }
81         cout<<endl;
82     }
83     return 0;
84 }

 2015—04-30更新代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<queue>
 9 using namespace std;
10 #define NN 50050
11 int n;
12 int a[NN],ans[NN];
13 int lowbit(int k)
14 {
15     return k&(-k);
16 }
17 void begin()
18 {
19     for(int i=1;i<=n;i++)
20     {
21         a[i]= lowbit(i);
22     }
23 }
24 int query(int x)
25 {
26     int sum=0;
27     while(x>0)
28     {
29         sum=sum+a[x];
30         x=x-lowbit(x);
31     }
32     return sum;
33 }
34 void update(int x)
35 {
36     while(x<=n)
37     {
38         a[x]=a[x]-1;
39         x=x+lowbit(x);
40     }
41 }
42 void add(int y,int x)
43 {
44     int l = x,r = n,mid;
45     while(l<r)
46     {
47         mid = (l+r)/2;
48       if(query(mid)>=x) r = mid;
49       else l = mid+1;
50     }
51     update(l);
52     ans[y] = l;
53 }
54 int main()
55 {
56     int i,j,k,t,m,x;
57     while(cin>>n)
58     {
59         begin();
60         for(i=1;i<=n;i++)
61         {
62             cin>>x;
63             add(i,x);
64         }
65         cin>>m;
66         for(j=1; j<=m; j++)
67         {
68            cin>>x;
69             if(j==1) cout<<ans[x];//注意格式啊,第二次提交格式错了一次,呜呜
70            else cout<<" "<<ans[x];
71         }
72         cout<<endl;
73     }
74     return 0;
75 }
View Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值