• 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 ktickets 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 a1a2, ..., 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, q1q2, ..., 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
    

    Author: LI, Dinghua
    Contest: ZOJ Monthly, August 2012

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4803

    用树状数组各种坑人.....

    最后文哥告诉我用线段树。

    View Code
     1 #include <stdio.h>
     2 #include <malloc.h>
     3 #define maxn 500005
     4 
     5 int ans[maxn],  l;
     6 
     7 struct node
     8 {
     9   int num, i, j;
    10   node *lchild, *rchild;
    11 };
    12 
    13 typedef  node* link;
    14 link head=NULL;
    15 
    16 link creatTree(int a, int b)
    17 {
    18     link r;
    19     if(b<a) return NULL;
    20     r=(link)malloc(sizeof(node));
    21     r->i=a;  r->j=b; r->num=b-a;
    22     if(b-a>1)
    23     {
    24         r->lchild=creatTree(a,(a+b)/2);
    25         r->rchild=creatTree((a+b)/2,b);
    26     }
    27     else
    28         r->lchild=r->rchild=NULL;
    29      return r;
    30 }
    31 
    32 void insertTree(link r,int now)
    33 {
    34     if(!r->lchild && !r->rchild)
    35     {
    36          r->num--;
    37         ans[l++] = r->i;
    38         return ;
    39     }
    40     if(r->lchild->num < now)
    41     {
    42         now = now - r->lchild->num;
    43          r->num--;
    44         insertTree(r->rchild,now);
    45         return ;
    46     }
    47     if(r->lchild->num >= now )
    48     {
    49          r->num--;
    50         insertTree(r->lchild,now);
    51     }
    52     return ;
    53 }
    54 
    55 int main()
    56 {
    57     int n, m, i, j,now;
    58     while(~scanf("%d",&n))
    59     {
    60         head=NULL;
    61         head=creatTree(1,n+1);
    62         l=1;
    63         for(i=0;i<n;i++)
    64         {
    65             scanf("%d",&m);
    66             now = m;
    67             insertTree(head,now);
    68         }
    69         scanf("%d",&m);
    70         for(i=0;i<m;i++)
    71         {
    72             scanf("%d",&n);
    73             if(i!=m-1) printf("%d ",ans[n]);
    74             else printf("%d\n",ans[n]);
    75         }
    76     }
    77     return 0;
    78 }

      

    可以用树状数组记录当前位置的现在的编号 树状数组 + 二分查找..

     

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define maxn 50050
     4 int hash[maxn],c[maxn],seat[maxn],n;
     5 
     6 int lowbit(int i)
     7 {
     8     return i & (-i);
     9 }
    10 
    11 int getSum(int x)
    12 {
    13     int i, sum=0;
    14     for(i = x; i > 0; i -= lowbit(i))
    15       sum += c[i];
    16     return sum;  
    17 }
    18 
    19 void mod(int x, int data)
    20 {
    21     int i;
    22     for(i = x; i <= n; i += lowbit(i))
    23      c[i] += data; 
    24 }
    25 
    26 int find(int ans)
    27 {
    28     int s=1, t=n, mid, s1,s2;
    29     while(s <= t)
    30     {
    31         mid = (s+t)/2;
    32         
    33         s1 = getSum(mid);
    34         s2 = getSum(mid-1);
    35         if(s1 == ans && s2 == ans-1) return mid;
    36         else if(s1 < ans) s = mid + 1;
    37         else t = mid -1; 
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     int m, i, mid, end, now, ans, mark;
    44     while(~scanf("%d",&n))
    45     {
    46         memset(hash,0,sizeof(hash));
    47         memset(seat,0,sizeof(seat));
    48         for(i=1;i<=n;i++)
    49         mod(i,1);
    50         for(i=1;i<=n;i++)
    51         {
    52             scanf("%d",&ans);
    53             ans=find(ans);
    54             seat[i]=ans;
    55             mod(ans,-1);    
    56         }
    57         scanf("%d",&m);
    58         for(i=0;i<m;i++)
    59         {
    60             scanf("%d",&ans);
    61             if(i==m-1) printf("%d\n",seat[ans]);
    62             else printf("%d ",seat[ans]);
    63         }
    64     }
    65     return 0;
    66 }

      

    我自己写了很久的记录的而是当前位置前面已经被占用了的位置的数目,然后当前位置的编号即为  自身 - 前面已将占用的数

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define maxn 50050
     4 int c[maxn],seat[maxn],n;
     5 
     6 int lowbit(int i)
     7 {
     8     return i & (-i);
     9 }
    10 
    11 int getSum(int x)
    12 {
    13     int i, sum=0;
    14     for(i = x; i > 0; i -= lowbit(i))
    15       sum += c[i];
    16     return sum;
    17 }
    18 
    19 void mod(int x, int data)
    20 {
    21     int i;
    22     for(i = x; i <= n; i += lowbit(i))
    23      c[i] += data;
    24 }
    25 
    26 int find(int mark, int n, int ans)
    27 {
    28     int s=mark, t=n, mid, s1,s2;
    29     while(s <= t)
    30     {
    31         mid = (s + t)/2;
    32 
    33         s1 = mid - getSum(mid);
    34         s2 = mid - 1 - getSum(mid -1);
    35         if(s1 == ans && s2 == ans-1) return mid;
    36         else if(s1 < ans)   s = mid +1;
    37         else t = mid + 1;
    38     }
    39     return -1;
    40 }
    41 
    42 int main()
    43 {
    44     int m, i, mid, end, now, ans, mark;
    45     while(~scanf("%d",&n))
    46     {
    47         memset(c,0,sizeof(c));
    48         memset(seat,0,sizeof(seat));
    49         for(i=1;i<=n;i++)
    50         {
    51             scanf("%d",&ans);
    52             mark = ans + getSum(ans);
    53             mark = find(mark,n,ans);
    54             seat[i]=mark;
    55             mod(mark,1);
    56         }
    57         scanf("%d",&m);
    58         for(i=0;i<m;i++)
    59         {
    60             scanf("%d",&ans);
    61             if(i==m-1) printf("%d\n",seat[ans]);
    62             else printf("%d ",seat[ans]);
    63         }
    64     }
    65     return 0;
    66 }

      

    写了好久....终于写了个时间是 540ms 的了...  只是内存很大...

    View Code
     1 #include <stdio.h>
     2 #define maxn 50005
     3 struct node
     4 {
     5     int i, j, num;
     6 };
     7 struct node r[3*maxn];
     8 int st[maxn];
     9 
    10 void createTree(int s, int t, int i)
    11 {
    12     if(s == t)
    13     {
    14         r[i].i = s;
    15         r[i].j = t;
    16         r[i].num = t - s + 1;
    17         return ;
    18     }
    19     createTree(s, (s + t)/2, i*2);
    20     createTree((s + t)/2+1, t, i*2+1);
    21     r[i].i = s;
    22     r[i].j = t;
    23     r[i].num = t - s + 1;
    24 }
    25 
    26 int find(int now)
    27 {
    28     int i=1;
    29     while(r[i].i != r[i].j)
    30     {
    31         if(r[i*2].num < now)
    32         {
    33             now -= r[i*2].num;
    34             r[i].num--;
    35             i = i * 2 + 1;
    36         }
    37         else
    38         {
    39             r[i].num--;
    40             i = i * 2;
    41         }
    42     }
    43     r[i].num--;
    44     return r[i].i;
    45 }
    46 
    47 int main()
    48 {
    49     int n, m, i, ans;
    50     while(~scanf("%d",&n))
    51     {
    52         for(i = 0; i < maxn; i++) st[i] = 0;
    53         createTree(1, n, 1);
    54         for(i = 1; i <= n; i++)
    55         {
    56             scanf("%d",&ans);
    57             st[i] = find(ans);
    58         }
    59         scanf("%d",&m);
    60         for(i = 1; i <= m; i++)
    61         {
    62             scanf("%d",&ans);
    63             if(i==m) printf("%d\n",st[ans]);
    64             else printf("%d ",st[ans]);
    65         }
    66     }
    67     return 0;
    68 }

      

  • 相关阅读:
    列表的内置方法
    oracle安装教程
    大二暑假假期周进度01
    win 10安装Linux虚拟机教程
    需求工程——软件建模与分析阅读笔记05
    网络爬虫阅读笔记01
    Java 测试连接Oracle数据库是否成功,ojdbc7.jar包下载
    河北创新年报统计平台系统整体界面展示,与数据下钻
    java 三大框架 struct2部分 实现增删该查操作
    itextsharp生成pdf
  • 原文地址:https://www.cnblogs.com/yoru/p/2657709.html
Copyright © 2020-2023  润新知