UVA - 1513 Movie collection (树状数组)
题意:有n个影碟,标号为1~n,位置为0~n-1,每次取出一个影碟看完后,将其放在最前面(标号为0处),问每个影碟取出前,其位置之前有多少个影碟。
分析:
1、数组大小开为100000*2,后100000个位置放初始的n个影碟,每次看完一个影碟后,依次向前放在前100000个位置中。
2、将放置影碟处标记为1---add(x, 1),当影碟取出时,再add(x,-1)。
3、通过sum(x)-1可知当前位置之前有多少个影碟。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1000;
const double pi = acos(-1.0);
const int MAXN = 200000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int n, m;
int pos[MAXN];
int sum(int x){
int ans = 0;
for(int i = x; i >= 1; i -= lowbit(i)){
ans += a[i];
}
return ans;
}
void add(int x, int value){
for(int i = x; i < MAXN; i += lowbit(i)){
a[i] += value;
}
}
vector<int> ans;
int main(){
int T;
scanf("%d", &T);
while(T--){
ans.clear();
memset(a, 0, sizeof a);
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i){
pos[i] = 100000 + i;
add(pos[i], 1);
}
int cnt = 100000;
while(m--){
int k;
scanf("%d", &k);
ans.push_back(sum(pos[k]) - 1);
add(pos[k], -1);
pos[k] = cnt--;
add(pos[k], 1);
}
int len = ans.size();
for(int i = 0; i < len; ++i){
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}
UVA - 1513 Movie collection (树状数组)的更多相关文章
- UVA 1513 - Movie collection(树状数组)
UVA 1513 - Movie collection option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...
- UVA 11423 - Cache Simulator (树状数组)
UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...
- Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)
Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...
- UVA 1428 - Ping pong(树状数组)
UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...
- uva 12356 Army Buddies 树状数组解法 树状数组求加和恰为k的最小项号 难度:1
Nlogonia is fighting a ruthless war against the neighboring country of Cubiconia. The Chief General ...
- UVA 11525 Permutation (树状数组+YY)
题意:给你k个数Si,然后给你一个等式 H= ∑ Si ∗ (K − i)! (i=(1->k)且0 ≤ Si ≤ K − i). 叫你求出第H个全排列 其实这是一个康托展开:X=a[n ...
- UVA - 1471 Defense Lines 树状数组/二分
Defense Lines After the last war devastated your country, you - as the ...
- LA 5902 - Movie collection 树状数组(Fenwick树)
看题传送门 题目大意:XXX喜欢看电影,他有好多好多的影碟,每个影碟都有个独立的编号.开始是从下往上影碟的顺序是n~1,他每次拿出影碟的时候,你需要输出压在该影碟上的有几个.(拿出后其他影碟顺序不变) ...
- UVA 1513 Movie collection (树状数组+反向存储)
题意:给你n盘歌碟按照(1....n)从上到下放,接着m个询问,每一次拿出x碟,输出x上方有多少碟并将此碟放到开头 直接想其实就是一线段的区间更新,单点求值,但是根据题意我们可以这样想 首先我们倒着存 ...
- UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
随机推荐
- java#临时文件目录
String tmpDir=System.getProperty("java.io.tmpdir");
- java 调用阿里云短信接口,报InvalidTimeStamp.Expired : Specified time stamp or date value is expired.
官网解释: 问题所在: 自己的电脑(或者服务器) 的时间与阿里云的服务器时间 相差15分钟了. 解决方法 : 把自己的电脑时间 (或者服务器)的时间 改成标准的北京时间就行了.
- 设计模式课程 设计模式精讲 18-2 迭代器模式coding
1 代码演练 1.1 代码演练1(迭代器模式演练) 1.2 代码使用场景 1 代码演练 1.1 代码演练1(迭代器模式演练) 需求: 课程管理:需要实现课程可进行增添,删除,并能够打印出课程列表. u ...
- [Write-up]Mr-Robot
关于 下载地址 目标:找到3个Key 哔哩哔哩视频. 信息收集 用的是Host-only,所以网卡是vmnet1,IP一直是192.168.7.1/24 nmap -T4 192.168.7.1/24 ...
- redis之Set(有序)类型常用方法总结
redis之Set(有序)类型常用方法总结 存--ZADD key [NX|XX] [CH] [INCR] score member [score member ...] XX: 仅仅更新存在的成员, ...
- ionic3记录之栅格布局使文字居中
{ display:flex; align-items:center; } 未完待续...
- 标准模板库中的向量(vector)
//C++数据结构与算法(第4版) Adam Drozdek 著 徐丹 吴伟敏<<清华大学出版社>> 头文件:#include<vector> 向量是最简单的S ...
- Linux centos7 awk工具
一.awk介绍 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk含盖sed所有功能,把文件逐行的读入,以空格为默认分隔 ...
- mutiset的简单介绍转载
原文链接:https://blog.csdn.net/sodacoco/article/details/84798621 c++语言中,multiset是<set>库中一个非 ...
- 1-m*n循环填数(用标记数组)blibli2018秋招第三题一个类型
#include <iostream> #include<string> #include<algorithm> using namespace std; ; in ...