poj 1281 MANAGER

MANAGER
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2654   Accepted: 955

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

Source

Southeastern Europe 2002


题意:首先给你最大耗费值max,然后n个要显示的进程的编号,然后输出命令。a代表插入消耗为x的进程,p代表删除策略,如果p为1则为删除耗费最小的进程,p为2则删除耗费最大的进程,r代表以当前策略删除进程,e代表结束。

做法:首先建立一个vector,a的话直接存入容器中,r的话首先判断容器的size是否为0,为0直接输出-1,然后如果p=1,则直接搜索最小值的位置,p=2则搜索最大值的位置,然后删除。最后要注意一组数据之后要再加一个换行。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
    int max1,min1;
    vector<int>num;
    int vis[10005];
    int i,j,k,n,t,key,flag;
    int js;
    char c;
    js=0;
    while(scanf("%d",&max1)!=EOF)
    {
        t=1;
        num.clear();
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&k);
            vis[k]=1;
        }
        flag=1;
        while(1)
        {
            scanf("%c",&c);
            if(c=='a')
            {
                scanf("%d",&k);
                num.push_back(k);
            }
            if(c=='p')
            {
                scanf("%d",&flag);
            }
            if(c=='r')
            {
                if(num.size()==0)
                {
                    printf("-1\n");
                    continue;
                }
                else
                {
                    if(flag==1)
                    {
                        min1=10005;
                        for(i=0;i<num.size();i++)
                        {
                            if(num[i]<min1)
                            {
                                min1=num[i];
                                key=i;
                            }
                        }
                        num.erase(num.begin()+key);
                    }
                    if(flag==2)
                    {
                        min1=0;
                        for(i=0;i<num.size();i++)
                        {
                            if(num[i]>min1)
                            {
                                min1=num[i];
                                key=i;
                            }
                        }
                        num.erase(num.begin()+key);
                    }
                }
                if(vis[t++]==1)
                    printf("%d\n",min1);
            }
            if(c=='e')
                break;
        }
        printf("\n");
    }
}


阅读更多

没有更多推荐了,返回首页