• ZOJ 3635 Cinema in Akiba【树状数组+二分查找】


    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
    

    题目大意:如果有k剩票,您的机票号码将整数我(1≤我≤k),你应该选择第i空的座位(不被他人)和坐下来为电影。给出m, m次询问,每次p顾客最后的位置;


    思路:树状数组+二分查找; 感觉树状数组做的题目还是太少了,或者没有找到树状数组的真谛,加强学习;
    开始的时候都是空位,那么add(i, 1);统计i位置前面的空座位;对于每次i空位置被占一个,那么i位置之后的每个位置的空位少了一个,那么add(i, -1);
    对于找和sum的位置的时候用到二分查找, 超找出和为i的位置,L=1, R=n, 每次计算SUM(mid)进行更新左右点;

    代码如下:

    View Code
    #include<string.h>
    #include<stdio.h>
    #include<stdio.h>
    #include<string.h>
    #define N 50005 
    int c[N], n, place[N]; 
    int Lowbit(int t)  
    {
        return t&(t^(t-1));  
    }
    int Sum(int end)   
    {
        int sum = 0;
        while(end > 0)
        {
            sum += c[end];
            end -= Lowbit(end);
        }
        return sum;
    }
    void add(int li, int val) 
    {
        while(li<=n)
        {
            c[li] += val;
            li += Lowbit(li);
        }
    } 
    int main()
    {
        int m, i, j, q, a;
        while(scanf("%d", &n)!=EOF)
        {
            memset(place, 0, sizeof(place));
            memset(c, 0, sizeof(c)); 
            for(i=1; i<=n; i++)
                add(i, 1);
            for(i=1; i<=n; i++)
            {
                scanf("%d", &a);
                int l=1, r=n;
                while(l<r)
                {
                    int mid=(l+r)/2;
                    if(Sum(mid)>=a)
                        r=mid;
                    else
                        l=mid+1;
                }
                place[i]=l;
                add(l, -1);
            }
            scanf("%d", &m);
            for(i=1; i<=m; i++)
            {
                scanf("%d", &q); 
                if(i!=m)
                    printf("%d ", place[q]);
                else
                    printf("%d\n", place[q]);
            }
        }
    }
  • 相关阅读:
    Android简易抽屉效果
    apk 下载并自动安装
    python2.7_1.3_获取远程设备的IP地址
    python2.7_1.2_打印设备名和IPv4地址
    python2.7_1.14_编写一个简单的回显客户端/服务器应用
    python2.7_1.13_编写一个SNTP客户端
    C语言程序转换为Python语言
    5分钟弄懂Docker!
    让正则表达式变简单(PythonVerbalExpressions)
    python sqlalchemy-migrate 使用方法
  • 原文地址:https://www.cnblogs.com/Hilda/p/2657744.html
Copyright © 2020-2023  润新知