POJ 1281 MANAGER

本文介绍了一个基于特定管理策略的进程管理模拟程序,该程序能够处理添加、移除进程请求,并根据最小或最大成本策略移除进程。同时,文章提供了一个C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MANAGER
Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u

Description

One of the programming paradigm in parallel processing is the producer/consumer paradigm that can be implemented using a system with a "manager" process and several "client" processes. The clients can be producers, consumers, etc. The manager keeps a trace of client processes. Each process is identified by its cost that is a strictly positive integer in the range 1 .. 10000. The number of processes with the same cost cannot exceed 10000. The queue is managed according to three types of requests, as follows:
  • a x - add to the queue the process with the cost x;
  • r - remove a process, if possible, from the queue according to the current manager policy;
  • p i - enforce the policy i of the manager, where i is 1 or 2. The default manager policy is 1
  • e - ends the list of requests.

There are two manager policies:
  • 1 - remove the minimum cost process
  • 2 - remove the maximum cost process

The manager will print the cost of a removed process only if the ordinal number of the removed process is in the removal list.

Your job is to write a program that simulates the manager process.

Input

The input is from the standard input. Each data set in the input has the following format:
  • the maximum cost of the processes
  • the length of the removal list
  • the removal list - the list of ordinal numbers of the removed processes that will be displayed; for example 1 4 means that the cost of the first and fourth removed processes will be displayed
  • the list of requests each on a separate line.

Each data set ends with an e request. The data sets are separated by empty lines.

Output

The program prints on standard output the cost of each process that is removed, provided that the ordinal number of the remove request is in the list and the queue is not empty at that moment. If the queue is empty the program prints -1. The results are printed on separate lines. An empty line separates the results of different data sets.

An example is given in the following:

Sample Input

5
2
1 3
a 2
a 3
r
a 4
p 2
r
a 5
r
e

Sample Output

2
5


 

题目大意:

线程模拟。

ax——将一个花费为x的进程加到队列中

r——如果可能,按照当前管理者的策略,删除一个进程

p i ——执行管理者的策略i,其中i是1或者2,缺省值为1

e——请求列表终止

两个管理者的策略为:

1——删除最小耗费进程

2——删除最大耗费进程

 

输出指定的删除序列

#include <iostream>
#include<algorithm> 
using namespace std;
int cmp1(int a,int b)
{
	return a>b;
}
int cmp2(int a,int b)
{
	return a<b;
}
int main()
{
	
    int num;
    while(cin>>num&&num)
	{
		int p=1;
		int n;
		cin>>n;
		int a[1010]={0},b[1010]={0},c[2010]={0};
		int i;
		for(i=1;i<=n;i++)
			cin>>b[i];
		int a1=1,b1=1,c1=1;
		char ch;
		i=0;    
		while(cin>>ch&&ch!='e')
		{
			if(ch=='a')
			{
				cin>>a[a1];
				a1++;
			}
			if(ch=='p')
				cin>>p;
			if(ch=='r')
			{
				if(p==1)//删除最小进程
				{
					sort(a+1,a+a1,cmp1);
					c[c1]=a[a1-1];
					c1++;
					a1=a1-1;
				}
				if(p==2)//删除最大进程
				{
					sort(a+1,a+a1,cmp2);
					c[c1]=a[a1-1];
					c1++;
					a1=a1-1;
				}
			}
		}
		for(i=1;i<=n;i++)
			cout<<c[b[i]]<<endl;
		cout<<endl;
	}
	return 0;
}

/*
5
2
1 3
a 2
a 3
r
a 4
p 2
r
a 5
r
e
*/


刚开始提交WrongAnswer 后来注意到时sort函数的使用,数组开始下标从0开始还是从1开始sort括号里的的列表不同,

sort(a+1,a+a1,cmp1);
我的下标从1开始。

sort函数详情见http://blog.youkuaiyun.com/sunshumin/article/details/37756027

再提交时是PE错误,改成一次while循环加一个空行,ac。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值