题目意思:
0 结束系统
1 K P 把一个叫K的客户,加入系统。他的优先级是P
2 输出最高优先级的客户名字, 同时删掉他
3 输出最低优先级的客户名字,同时删掉他
想法: 平衡树,找最大值最小值即可。
C++这次我试了试multimap,挺好玩的。实际上这题也应该没重复关键字吧~
iterator 迭代器的begin()肯定是最小值
end() -- 肯定是最大值。 空可以用 empty之类判断都可以。 我用的是begin() == end()为空
用STL程序非常简短
时间也很快
3481 | Accepted | 776K | 516MS | G++ |
#include <iostream>
#include <cstdio>
#include <map>
#include <cstdlib>
using namespace std;
multimap<int, int>G;
multimap<int ,int>::iterator it;
int main()
{
int flag;
while (1)
{
scanf("%d", &flag);
if (flag == 1)
{
int k, p;
scanf("%d%d", &k, &p);
G.insert(make_pair(p, k));
}
if (flag == 3)
{
it = G.begin();
if (it == G.end()) printf("0\n");
else {
printf("%d\n", it -> second);
G.erase(it);
}
}
if (flag == 2)
{
it = G.end();
if (G.begin() == it) printf("0\n");
else{
--it;
printf("%d\n", it -> second);
G.erase(it);
}
}
if (flag == 0) break;
}
}
=============
然后是自己写的SPLAY,和STL的速度差不多
3481 | Accepted | 760K | 547MS | G++ |
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef pair<int, int> PII;
int maxint = 0x7fffffff;
struct node
{
PII key;
node *c[2];
int size;
node()
{
key = make_pair(0, 0);
size = 0;
c[0] = c[1] = this;
}
node(PII KEY_, node *c0, node *c1)
{
key = KEY_;
c[0] = c0;
c[1] = c1;
}
node* rz(){return size = c[0] -> size + c[1] -> size + 1, this;}
}Tnull, *null = &Tnull;
struct splay
{
node *root;
splay()
{
root = (new node(*null)) -> rz();
root -> key = make_pair(maxint, maxint);
}
void zig(int d)
{
node *t = root -> c[d];
root -> c[d] = null -> c[d];
null -> c[d] = root;
root = t;
}
void zigzig(int d)
{
node *t = root -> c[d] -> c[d];
root -> c[d] -> c[d] = null -> c[d];
null -> c[d] = root -> c[d];
root -> c[d] = null -> c[d] -> c[!d];
null -> c[d] -> c[!d] = root -> rz();
root = t;
}
void finish(int d)
{
node *t = null -> c[d], *p = root -> c[!d];
while (t != null)
{
t = null -> c[d] -> c[d];
null -> c[d] -> c[d] = p;
p = null -> c[d] -> rz();
null -> c[d] = t;
}
root -> c[!d] = p;
}
void select(int k)//谁有k个left儿子
{
int t;
while (1)
{
bool d = k > (t = root -> c[0] -> size);
if (k == t || root -> c[d] == null) break;
if (d) k -= t+ 1;
bool dd = k > (t = root -> c[d] -> c[0] -> size);
if (k == t || root -> c[d] ->c[dd] == null){zig(d); break;}
if (dd) k -= t+1;
d != dd ? zig(d), zig(dd) : zigzig(d);
}
finish(0), finish(1);
root -> rz();
}
void search(PII x)
{
while (1)
{
bool d = x > (root -> key);
if (root -> c[d] == null)break;
bool dd = x > (root -> c[d] -> key);
if (root -> c[d] -> c[dd] == null) {zig(d);break;}
d != dd ? zig(d), zig(dd) : zigzig(d);
}
finish(0), finish(1);
root -> rz();
if (x > root -> key) select(root -> c[0] -> size + 1);
}
void ins(PII x)
{
search(x);
node *oldroot = root;
root = new node(x, oldroot -> c[0], oldroot);
oldroot -> c[0] = null;
oldroot -> rz();
root -> rz();
}
void del(PII x)
{
search(x);
node *oldroot = root;
root = root -> c[1];
select(0);
root -> c[0] = oldroot -> c[0];
root -> rz();
delete oldroot;
}
PII sel(int k){return select(k - 1), root -> key;}
int ran(PII x){return search(x), root -> c[0] -> size + 1;}
}sp;
int n, tmp, ans;
int main()
{
int flag;
while (1)
{
scanf("%d", &flag);
if (flag == 1)
{
int k, p;//k客户是p
scanf("%d%d", &k, &p);
sp.ins(make_pair(p,k));
}
if (flag == 2)
{
if (sp.root->size == 1) printf("0\n");
else {
PII tmp;
tmp = sp.sel(sp.root -> size - 1);
printf("%d\n", tmp.second);
sp.del(tmp);
}
}
if (flag == 3)
{
if (sp.root -> size == 1) printf("0\n");
else{
PII tmp;
tmp = sp.sel(1);
printf("%d\n", tmp.second);
sp.del(tmp);
}
}
if (flag == 0) break;
}
}