J - MANAGER(2.4.5)

來源:互聯網
上載者:User

標籤:des   style   http   color   strong   os   

J - MANAGER(2.4.5) Time Limit:1000MS      Memory Limit:10000KB      64bit IO Format:%I64d & %I64uSubmit Status

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
//資料集合之間有空行。#include<iostream>#include<algorithm>using namespace std;int a[10000],i,b[10000],c,j,k,p,j1,mn,qq[100];int n,m;char s;void shanchu(){n++;   sort(a,a+k);   if(p==1)  {   for(i=0;i<j1;i++)   if(n==b[i])   {qq[mn]=a[0];mn++;break;}   a[0]=999999;   }   else   {   for(i=k-1;i>=0;i--)   if(a[i]!=999999)   {   m=a[i];   a[i]=999999;   break;   }   for(i=0;i<j1;i++)   if(n==b[i])   {qq[mn]=m;mn++;}   }}int main(){   while(cin>>n&&n)   {   mn=0;   n=0;   cin>>j1;   j=0;   i=j1;   k=0;   p=1;   while(i--)   cin>>b[i];   while(cin>>s)   {      if(s=='e')   break;   switch(s)   {  // case 'e':break;   case 'a':cin>>a[k],k++;break;   case 'r':shanchu();break;   case 'p':cin>>p;break;   }   }   for(i=0;i<j1;i++)   cout<<qq[i]<<endl;   }   cout<<endl;return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.