题意:
三种操作:
1. 插入对键值[key, value]
2. 输出最大的key 并删除。
3. 输出最小的key 并删除。
思路:
这显然是一个map。 其实一个水题, 拿map 模拟直接过了。 但是最近练习一下Splay。 就写炸了= =
很容易用Splay模拟 来实现。
第一种操作: 直接从根开始插结点即可。
第二种操作,最大的key 直接从当前根结点找后继。 转到根, 输出根的value,删除根结点。
第三种操作,找前驱, 转到根, 输出, 删除。
但是我的insert 函数, 没有处理好边界问题, 莫名错误, wa tle 了一天。
内存池都用上了= =
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 5000000 + 10;
int ch[maxn][2];
int cnt[maxn];
int val[maxn];
int ans[maxn];
int pre[maxn];
int root;
int tot;
int ss[maxn];
int top = 0;
void pushup(int r){
if (r == 0) return;
int lson = ch[r][0];
int rson = ch[r][1];
cnt[r] = cnt[lson] + cnt[rson] + 1;
}
void pushdown(int r){};
void Rotate(int x,int kind){
int y = pre[x];
pushdown(y);
pushdown(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind] ] = y;
if (pre[y]){
int p = ch[pre[y] ][1] == y;
ch[pre[y] ][p] = x;
}
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
pushup(y);
}
void Splay(int x,int f){
pushdown(x);
while(pre[x] != f){
pushdown(pre[pre[x]]);
pushdown(pre[x]);
pushdown(x);
if (pre[pre[x] ] == f){
int kind = ch[pre[x] ][0] == x;
Rotate(x, kind);
}
else {
int y = pre[x];
int kind = ch[pre[y] ][0] == y;
if (ch[y][kind] == x){
Rotate(x, !kind);
Rotate(x, kind);
}
else {
Rotate(y, kind);
Rotate(x, kind);
}
}
}
pushup(x);
if (f == 0) root = x;
}
void Select(int k,int f){
int cur = root;
while(1){
pushdown(cur);
if (cnt[ch[cur][0]] + 1 == k){
break;
}
else if (cnt[ch[cur][0]] < k) {
k -= (cnt[ch[cur][0]] + 1);
cur = ch[cur][1];
}
else cur = ch[cur][0];
}
Splay(cur, f);
pushup(cur);
}
void clear(int x){
ans[x] = val[x] = 0;
ch[x][0] = ch[x][1] = 0;
pre[x] = 0;
cnt[x] = 0;
}
void del(int x){
if (x == 0) return;
Splay(x, 0);
if (ch[x][1]){
int t = root;
root = ch[root][1];
Select(1, 0);
ch[root][0] = ch[t][0];
if (ch[root][0])
pre[ch[root][0] ] = root;
}
else root = ch[x][0];
pre[root] = 0;
pushup(root);
ss[++top] = x;
clear(x);
}
void newnode(int& r, int f,int p,int k){
if (top >= 1){
r = ss[top];
--top;
}
else {
r = ++tot;
}
pre[r] = f;
cnt[r] = 1;
ch[r][0] = ch[r][1] = 0;
val[r] = p;
ans[r] = k;
}
void insert(int k,int p){
if (root == 0){ /// 要处理好边界, 这里没加 wa,tle 了一上午加 一下午T_T!!!
newnode(root, 0, p, k);
return;
}
int cur = root;
while(ch[cur][val[cur] < p ]){
pushdown(cur);
if (val[cur] == p){
ans[cur] = k;
Splay(cur, 0);
return;
}
cur = ch[cur][val[cur] < p ];
}
newnode(ch[cur][val[cur] < p ], cur, p, k);
cur = ch[cur][val[cur] < p ];
Splay(cur, 0);
pushup(cur);
}
//int get_max(int x){
// while(ch[x][1]){
// x = ch[x][1];
// }
// Splay(pre[x],0);
// return ans[pre[x] ];
//}
//int get_min(int x){
//
// while(ch[x][0]){
// x = ch[x][0];
// }
// Splay(pre[x],0);
// return ans[ pre[x] ];
//}
const int inf = 0x3f3f3f3f;
void dfs(int cur){
if (cur == 0) return;
dfs(ch[cur][0]);
printf("结点:%d, 左儿子:%d, 右儿子:%d, 节点数量:%d, val = %d, ans = %d\n", cur, ch[cur][0], ch[cur][1], cnt[cur], val[cur], ans[cur]);
dfs(ch[cur][1]);
}
void debug(){
printf("root : %d\n", root);
dfs(root);
}
int main(){
int q;
while(~scanf("%d",&q) && q){
if (q == 1){
int k,p;
scanf("%d %d",&k, &p);
insert(k, p);
// debug();
}
else if (q == 3){
// debug();
if (root == 0){
puts("0");
continue;
}
Select(1, 0);
printf("%d\n", ans[root]);
del(root);
// debug();
}
else if (q == 2){
if (root == 0){
puts("0");
continue;
}
Select(cnt[root], 0);
printf("%d\n", ans[root]);
del(root);
// debug();
}
}
return 0;
}
Double Queue
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:
Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy. Input Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority. Output For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output. Sample Input 2 1 20 14 1 30 3 2 1 10 99 3 2 2 0 Sample Output 0 20 30 10 0 Source |
[Submit] [Go Back] [Status] [Discuss]