Movie collection(2013.09.15)树状数组
来源:互联网 发布:阿里云 ads 编辑:程序博客网 时间:2024/05/21 16:57
I. Movie collection
Movie collection
Mr. K. I. has a very big movie collection. He has organized his collection in a big stack. Whenever he wants to watch one of the movies, he locates the movie in this stack and removes it carefully, ensuring that the stack doesn’t fall over. After he finishes watching the movie, he places it at the top of the stack.
Since the stack of movies is so big, he needs to keep track of the position of each movie. It is sufficient to know for each movie how many movies are placed above it, since, with this information, its position in the stack can be calculated. Each movie is identified by a number printed on the movie box.
Your task is to implement a program which will keep track of the position of each movie. In particular, each time Mr. K. I. removes a movie box from the stack, your program should print the number of movies that were placed above it before it was removed.
Input
On the first line a positive integer: the number of test cases, at most 100. After that per test case:
- one line with two integers m and r (1 ≤ m,r ≤ 100 000): the number of movies in the stack and the number of locate requests.
- one line with r integers a1,…,ar (1 ≤ ai ≤ m) representing the identification numbers of movies that Mr. K. I. wants to watch.
For simplicity, assume that the initial stack contains the movies with identification numbers 1, 2,…,m in increasing order, where the movie box with label 1 is the top-most box.
Output
Per test case:
- one line with r integers, where the i-th integer gives the number of movie boxes above the box with label ai, immediately before this box is removed from the stack.
Note that after each locate request ai, the movie box with label ai is placed at the top of the stack.
Sample in- and output
Input
Output
23 33 1 15 34 4 5
2 1 03 0 4
Copyright notice
This problem text is copyright by the NWERC 2011 jury. It is licensed under the Creative Commons Attribution-Share Alike license version 3.0; The complete license text can be found at: http://creativecommons.org/licenses/by-sa/3.0/legalcode
这是个树状数组。。我想了很久也木有想出来怎么处理。。太水了
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define maxn 201000using namespace std;int p[maxn],a[maxn];int lowbit(int x){ return x&-x;}void update(int x,int i){ while(x<maxn) { a[x]+=i; x+=lowbit(x); }}int sum(int x){ int ans=0; while(x>0) { ans+=a[x]; x-=lowbit(x); } return ans;}int main (){ int t,n,m,i; cin>>t; while(t--) { cin>>n>>m; memset(p,0,sizeof(p)); memset(a,0,sizeof(a)); for (i=1; i<=n; i++) { p[i]=n-i+1;//利用p[]数组来记录位置 update(i,1); } int pos,s=n; while(m--) { scanf("%d",&pos); if (m!=0) printf("%d ",n-sum(p[pos]));//pos代表第几张电影碟片,p[pos]代表着该碟片在树状数组当中的位置 else printf("%d\n",n-sum(p[pos])); update(p[pos],-1);//从p[pos]位置抽出碟片之后,相应的数组需要更新 p[pos]=++s;//代表着将pos碟片放在了顶上 update(p[pos],1);//更新数组 } } return 0;}
- Movie collection(2013.09.15)树状数组
- 【UVA】1513-Movie collection(树状数组)
- UVA1513 - Movie collection(树状数组)
- UVA 1513-Movie collection(树状数组)
- UVALive 5902 Movie collection(树状数组)
- UVA 1513 - Movie collection(树状数组)
- UVa 1513 Movie collection (树状数组)
- LA 5902 - Movie collection 树状数组(Fenwick树)
- UVA 1513—— Movie collection(树状数组)
- hnu 12423 movie collection#树状数组
- UVA 1513 - Movie collection(树状数组)
- uva 1513 - Movie collection(树状数组)
- LA 5902 Movie collection (树状数组)
- UVa 1513 / UVALive 5902 Movie collection (树状数组)
- SPOJ NWERC11C Movie collection 树状数组,线段树
- UVa 1513 / UVALive 5902 Movie collection (树状数组)
- uva 1513 - Movie collection--树状数组--预留前n个位置
- Movie collection
- QT 实现wsse username-token 实现?
- Beauty Contest(2013.09.15)(凸包)
- uva 11008 - Antimatter Ray Clearcutting(状态dp)
- CUDA的OpenCV-2.3.x/OpenCV-2.4.x编译
- 软件学院研究方向,对研究方向迷茫的同学一定要看(据说是一位北大软院前辈写的)
- Movie collection(2013.09.15)树状数组
- 初学者学Java(十五)
- JS如何获取url的参数
- A20 GSLX680 触摸按键驱动调试
- JDK 环境变量的配置
- NOR和NAND flash区别,RAM 和ROM区别
- CUDA下的GPU编程--线程和变量
- 使用html和CSS进行网页网站设计 -- 简明步骤
- C++中一条语句将文件内容写到std::string中