UVa 1513 Movie collection (树状数组)
2016-09-02 10:50
399 查看
题目链接:http://acm.hust.edu.cn/vjudge/problem/36169
题意:有n个数按1,2...,n从上到下排列,每次选择一个数,输出这个数上面有几个数,选择后的数放在最顶端。
思路:
在这个题中,每个数都有自己的位置,我们让位置编号从上到下依次递减,用数组C[i]表示第i个位置有几个数(只能是0或1),那么sum(i)就表示第1到第i个位置一共有几个数,即i以及它的下面一共有几个数,用n减去sum(i)即为答案。
题意:有n个数按1,2...,n从上到下排列,每次选择一个数,输出这个数上面有几个数,选择后的数放在最顶端。
思路:
在这个题中,每个数都有自己的位置,我们让位置编号从上到下依次递减,用数组C[i]表示第i个位置有几个数(只能是0或1),那么sum(i)就表示第1到第i个位置一共有几个数,即i以及它的下面一共有几个数,用n减去sum(i)即为答案。
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<vector> #include<algorithm> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lc rt<<1 #define rc rt<<1|1 using namespace std; typedef long long ll; const int maxn = 200000 + 10; const int INF = 1e9 + 10; int pos[maxn],C[maxn]; vector<int> ans; int m,n; int lowbit(int x) { return x & -x; } int sum(int x) { int cnt = 0; while(x > 0) { cnt += C[x]; x -= lowbit(x); } return cnt; } int add(int x, int p) { while(x < maxn) { C[x] += p; x += lowbit(x); } } int main() { int T; scanf("%d",&T); while(T--) { memset(C, 0, sizeof C); ans.clear(); scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) { pos[i] = n-i+1; add(pos[i],1); } int cnt = n; while(m--) { int x; scanf("%d",&x); ans.push_back(n-sum(pos[x])); add(pos[x],-1); pos[x] = ++cnt; add(pos[x],1); } for(int i = 0; i < ans.size(); i++) { if (i) printf(" "); printf("%d",ans[i]); } printf("\n"); } }
相关文章推荐
- UVA 1513-Movie collection(树状数组)
- UVA 1513 - Movie collection(树状数组)
- UVa 1513 / UVALive 5902 Movie collection (树状数组)
- UVA 1513 Movie collection (树状数组+反向存储)
- UVA 1513 - Movie collection(树状数组)
- 【UVA】1513-Movie collection(树状数组)
- UVA 1513 - Movie collection(树状数组)
- uva 1513 - Movie collection(树状数组)
- UVA 1513(树状数组)
- uva 1513 - Movie collection--树状数组--预留前n个位置
- 树状数组uva1513(好)
- UVa 1513 / UVALive 5902 Movie collection (树状数组)
- UVA - 1513(树状数组优化)
- UVA - 1513 Movie collection 树状数组
- UVa 1513 Movie collection (树状数组)
- UVA 1513—— Movie collection(树状数组)
- UVA 1513 树状数组
- UVA 501 - Black Box(二分 + 树状数组 + 离散化)
- uva 1428 - Ping pong(树状数组,4级)
- UVALive 4329 (树状数组)