poj 1281 MANAGER(简单模拟题)

来源:互联网 发布:犀牛软件 手机建模 编辑:程序博客网 时间:2024/05/17 22:30
MANAGER
点击打开题目链接
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2504 Accepted: 899

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

521 3a 2a 3ra 4p 2ra 5re

Sample Output

25

Source

简单的模拟题:

题意:

有如下几种操作符:

a x,添加一个花费为x的进程;

r 根据当前策略删除进程;

p x (x只能为1或2);

p 1:删除花费最小的进程(默认为此策略)

p 2:删除花费最大的进程;

输入:

最大的进程花费;

删除列表所能装的最大进程数

显示被删除进程的顺序数;

输入操作符,以‘e’结束;

输出:

要有空行分开各个数据;

输出被删除的进程(与在删除列表中存在的顺序数相同),则输出其花费,若无,输出-1;

利用到了容器的,显得习题比较简单了

代码:

#include <iostream>#include<string>#include<set>#include<vector>using namespace std;multiset<int> s;///set容器是有序的vector<int> v;multiset<int>::iterator it;//迭代器int policy;/*********************************//*****代码参考与网络**************//*********************************/ void a()//添加进程{    int cost;    cin>>cost;    s.insert(cost);}void re_policy(int policy)//根据当前策略删除{    switch(policy)    {    case 1:        if (s.empty())            cout << "-1" << endl;//当队列为空时输出-1;        else        {            it = s.begin();//找到花费最小的进程(set容器的开头处(因为其有序的))            v.push_back((*it));//向vector中添加进程(被删除)            s.erase(it);//在进程中移除        }        break;    case 2:        if(s.empty())            cout << "-1" << endl;        else        {            int size = s.size();            for (it = s.begin(); it != s.end(); it++)            {                size--;                if (size == 0)                {                    break;                }            }//找到set最后一个元素(即花费最大的元素)            v.push_back((*it));            s.erase(it);            break;        }    }}void p()//输入策略{    cin>>policy;}int main(){    int maxcost,re_num,i;    char oper;    //bool flag;    int re[1000];    while(cin>>maxcost>>re_num)//输入最大花费和删除列表的进程数    {        for(i=0;i<re_num;i++)            cin>>re[i];            //flag=false;            s.clear();            v.clear();           // count1=0;            policy=1;            while(cin>>oper&&oper!='e')//输入操作符            {                switch(oper)                {                case 'a':a();                    break;                case 'r':re_policy(policy);                    break;                case 'p':p();                    break;                }            }            for(i=0;i<re_num;i++)            {                cout<<v[re[i]-1]<<endl;//输出和删除列表中的顺序数相同的进程的花费            }            cout<<endl;    }    return 0;}


0 0
原创粉丝点击