//2_4_5: MANAGER 进程管理的模拟 POJ1281 ZOJ1371 UVA2514
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10000 + 10;
int spend[maxn],delet[maxn],queue[maxn];
void quick_sort_max(int st,int ed) //快排算法
{
int i = st,j = ed,temp;
if(st < ed)
{
temp = spend[st];
while(i != j)
{
while(j > i && spend[j] >= temp) j--;
spend[i] = spend[j];
while(i < j && spend[i] <= temp) i++;
spend[j] = spend[i];
}
spend[i] = temp;
quick_sort_max(st,i - 1);
quick_sort_max(i + 1,ed);
}
}
void quick_sort_min(int st,int ed) //快排算法
{
int i = st,j = ed,temp;
if(st < ed)
{
temp = spend[st];
while(i != j)
{
while(j > i && spend[j] <= temp) j--;
spend[i] = spend[j];
while(i < j && spend[i] >= temp) i++;
spend[j] = spend[i];
}
spend[i] = temp;
quick_sort_min(st,i - 1);
quick_sort_min(i + 1,ed);
}
}
int main()
{
int i,p,max,len,top,del;
char c;
while(scanf("%d",&max) != EOF)
{
scanf("%d",&len);
for(i = 0;i < len;i ++) scanf("%d",&delet[i]); //输入要删除的位数
top = -1;
del = p = 1;
while(scanf("%c",&c))
{
if(c == 'e') break;
else if(c == 'a') scanf("%d",&spend[++top]);
else if(c == 'p') scanf("%d",&p);
else if(c == 'r')
{
if(top < 0) queue[del++] = -1;
else
{
if(p == 1)
{
quick_sort_min(0,top);
queue[del++] = spend[top--];
}
else
{
quick_sort_max(0,top);
queue[del++] = spend[top--];
}
}
}
}
for(i = 0;i < len;i ++) printf("%d\n",queue[delet[i]]);
printf("\n");
}
return 0;
}
/*测试结果:通过POJ1281检测,ZOJ1371 UVA2514需要修改代码 输入数据之间还要空格
5
2
1 3
a 2
a 3
r
a 4
p 2
r
a 5
r
e
2
5
^Z
请按任意键继续. . .
*/POJ1281 ZOJ1371 UVA2514 MANAGER
最新推荐文章于 2020-03-11 00:05:59 发布
本文探讨了在进程管理中使用快排算法解决特定问题的方法,并提供了代码实现及测试结果。
1018

被折叠的 条评论
为什么被折叠?



