24h購物| | PChome| 登入
2013-07-05 15:27:47| 人氣1,157| 回應0 | 上一篇 | 下一篇

[UVA][BIT] 1513 - Movie collection

推薦 0 收藏 0 轉貼0 訂閱站台

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 n and m (1$ le$n, m$ le$100000): the number of movies in the stack and the number of locate requests.
  • one line with m integers a1,..., am (1$ le$ai$ le$n) representing the identification numbers of movies that Mr. K. I. wants to watch.


For simplicity, assume the initial stack contains the movies with identification numbers 1, 2,..., n in increasing order, where the movie box with label 1 is the top-most box.

Output 

Per test case:


  • one line with m 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 Input 

2
3 3
3 1 1
5 3
4 4 5

Sample Output 

2 1 0
3 0 4

題目描述:

一開始堆疊內部長成 (top) 1,2,3, ..., n (bottom), 接下來有 m 筆操作,
要將數字 x 搬到 top, 並且輸出它距離 top 的距離, 然後將 x 放在 top,
模擬所有操作

題目解法:

將 stack top 的屬性當作最大, 依序遞減到 bottom, 然後操作的時候查詢有多少比它大的數字,
接著將這個數字修改成 top 的屬性+1 放入,
這麼一來就可以使用 binary indexed tree 了


#include <stdio.h>
#include <string.h>
int BIT[262144];
void modify(int idx, int v, int L) {
while(idx <= L) {
BIT[idx] += v;
idx += idx&(-idx);
}
}
int query(int idx) {
int sum = 0;
while(idx) {
sum += BIT[idx];
idx -= idx&(-idx);
}
return sum;
}
int main() {
int A[100005];
int n, m, i, j;
int testcase;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &n, &m);
memset(BIT, 0, sizeof(BIT));
int L = n+m;
for(i = 1, j = n; i <= n; i++, j--) {
A[i] = j;
modify(A[i], 1, L);
}
int x, l, size = n;
for(i = 0; i < m; i++) {
scanf("%d", &x);
l = query(A[x]);
if(i) putchar(' ');
printf("%d", n-l);
modify(A[x], -1, L);
A[x] = ++size;
modify(A[x], 1, L);
}
puts("");
}
return 0;
}
 

台長: Morris
人氣(1,157) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][模擬] 12608 - Garbage Collection
此分類上一篇:[UVA][greedy] 12428 - Enemy at the Gates

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文