poj1281 multiset的应用

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3060   Accepted: 1081

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

读懂题目很关键,首先它要求队列是从小到大排序,1就是取最小值,2就是取最大值,然后把值记录在一个数组中,最后要求输出它给的序号元素的值,这里使用multiset,这是一个可重复的集合,与set的不可重复性相反,并且与set一样都是从小到大排好序的,它的插入删除操作都是都是O(logN),而且这里要注意迭代器的问题,begin,rbegin,end,rend的区别

c.begin() 返回一个迭代器,它指向容器c的第一个元素

c.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置

c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素

c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置

其他都是和stl模板类似的函数


#include <iostream>
#include <vector>
#include <set>
#include <cstdio>

using namespace std;
vector<int > a,b;
multiset<int >s;

void bs(int p)
{
    if(s.empty())
    {
        printf("-1\n");
        return ;
    }
    if(p==1)
    {
        multiset<int >::iterator k=s.begin();
        a.push_back(*k);
        s.erase(*k);
    }
    else
    {
        multiset<int >::reverse_iterator k=s.rbegin();
        a.push_back(*k);
        s.erase(*k);
    }
}
int main()
{
    char c[3];
    int m,n,i,j,x,k,p;
    scanf("%d%d",&m,&n);
    for(i=0;i<=n-1;i++)
    {
        scanf("%d",&x);
        b.push_back(x);
    }
    p=1;
    while(scanf("%s",c))
    {
        if(c[0]=='e') break;
        if(c[0]=='a')
        {
            scanf("%d",&x);
            s.insert(x);
        }
        else if(c[0]=='p')
        {
            scanf("%d",&x);
            p=x;

        }
        else
        {
            bs(p);
        }
       /* printf("p==%d\n",p);
         multiset<int >::iterator k=s.begin();
         printf("-----");
         while(k!=s.end())
         {
             printf("%d ",*k++);
         }
         printf("\n");*/
    }
    for(i=0;i<=n-1;i++)
    {
        k=b[i];
        printf("%d\n",a[k-1]);
    }
}

猜你喜欢

转载自blog.csdn.net/keepcoral/article/details/80037424
今日推荐