题目
in1
3
0 1
0 2
2
out1
2
in2
6
0 1
0 2
1
2
0 2
2
out2
1
2
思路1,只用线性表
用了一个链表栈存放题目数据
又开了个链表队列来存放难度和对应的频率(对应的查询复杂度是n)
代码1
#include<iostream>
using namespace std;
struct dot
{
int difficulty;
int frequency;
dot* prev;
dot* next;
dot()
{
difficulty = frequency = 0;
prev = next = NULL;
}
};
struct stack
{
dot* tail = NULL;
void push(int n)
{
if(!tail)
{
tail = new dot;
tail->prev = NULL;
tail->difficulty = n;
}
else
{
dot* temp = new dot;
temp->difficulty = n;
temp->prev = tail;
tail = temp;
}
}
int pop()
{
int ret = 0;
if(!tail)
return 0;
dot*temp = tail;
tail = tail->prev;
ret = temp->difficulty;
delete temp;
return ret;
}
};
struct list
{
dot* head;
dot* tail;
list()
{
head = tail = new dot;
}
void search()
{
cout << tail->difficulty << endl;
}
void out(int dif)
{
dot* record = tail;
while(record != head)
{
if(record->difficulty == dif)
{
record->frequency--;
if(record->frequency == 0)
{
if(record == tail)
{
tail = record->prev;
}
else
{
record->prev->next = record->next;
record->next->prev = record->prev;
}
delete record;
break;
}
break;
}
record = record->prev;
}
}
void insert(int dif)
{
dot* record = tail;
if(dif > record->difficulty)
{
dot* temp = new dot;
temp->prev = tail;
temp->next = NULL;
temp->difficulty = dif;
temp->frequency = 1;
tail->next = temp;
tail = temp;
return;
}
while(record != head)
{
if(record->difficulty == dif)
{
record->frequency++;
break;
}
else if(record->difficulty > dif && record->prev->difficulty < dif)
{
dot* temp = new dot;
temp->next = record;
temp->prev = record->prev;
temp->difficulty = dif;
temp->frequency = 1;
record->prev->next = temp;
record->prev = temp;
break;
}
record = record->prev;
}//
}
};
int main()
{
int n;
cin >> n;
stack one;
list two;
for(int i = 0; i < n; i++)
{
int c;
cin >> c;
switch(c)
{
case 0:
int dif;
cin >> dif;
one.push(dif);
two.insert(dif);
break;
case 2:
two.search();
break;
case 1:
int a = one.pop();
two.out(a);
break;
}
}
return 0;
}
思路2
然而操作到1e6就超时了,所以为了满足插入和查询都满足logn的操作的话,只能用平衡二叉树了,等后面学了再敲
思路3
突然发现这玩意删除操作没多少次,直接线性查找难度最大的题目即可。。
#include<iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int hardest = 0, size = 0, hardest_num = 0, ptr = -1;
int question[N];
int order, difficulty;
for(int i = 0;i != N;++i)
{
cin>>order;
switch(order)
{
case 0:
cin>>difficulty;
++ptr;
++size;
question[ptr] = difficulty;
if(difficulty == hardest)
++hardest_num;
if(difficulty > hardest)
{
hardest = difficulty;
hardest_num = 1;
}
break;
case 1:
if(size > 0)
{
--size;
if(hardest == question[ptr])
{
--hardest_num;
if(hardest_num == 0)
{
hardest = -1;
hardest_num = 0;
for(int i = 0;i != size;++i)
{
if(question[i] == hardest)
++hardest_num;
if(question[i] > hardest)
{
hardest = question[i];
hardest_num = 1;
}
}
}
}
--ptr;
if(size == 0)
{
hardest = 0;
hardest_num = 1;
}
}
break;
case 2:
cout<<hardest<<endl;
}
}
}