MANAGER POJ1281 C language

Source: Internet
Author: User

One of the programming paradigm in parallel processing is the Producer/consumer paradigm so can be implemented using a s Ystem 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 was identified by its cost, which 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 is and manager policies:
    • 1-remove the minimum cost process
    • 2-remove the maximum cost process

The manager would print the cost of a removed process only if the ordinal number of the removed process are in the removal l Ist.

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

Input

The input is from the standard input. Each data set of 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 would be displayed; For example 1 4 means then the cost of the first and fourth removed processes would be displayed
    • The list of requests each in a separate line.

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

Output

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

An example are given in the following:

Sample Input

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

Sample Output

25

I don't care how to tune in Uvalive, Zoj on the pass, Uvalive is the Wa,zoj is PE, I estimate is the problem of the output format, but I have used up my imagination I really do not know how to output, English is not good, do not read understand.

The problem is simulation, because I do not know how many data will come in, so I store the removal of the list and the process of the array is used to dynamically allocate memory, after the use of the release, and then re-apply. The size of the array of stored processes can be allocated according to the maximum cost of the process, and the array that stores the removed list is allocated according to the number of topics given.

1 //removal list is the output of the I-Remove process2 //such as 1, 3, outputs the 1th and 3rd removed processes3#include <stdio.h>4#include <malloc.h>5#include <string.h>6 7 intMaxcost;//The maximum cost of the processes8 intp=1;//Policy9 intRlength;//The length of the processTen int*rlist=null;//data used to store the removed list One CharOp//Operation A int*pro=null;//Process Array - intj,k=0;//J is removed for the first few times, K is the flag that was removed successfully, 0 is no, 1 is -  the voidRe () { -     intI,a; -     if(p==1){ -          for(i=0; i<maxcost;i++){ +             if(pro[i]!=0){//Queue non-empty -A=Pro[i]; +pro[i]=0; AJ + +; atk=1; -             } -         } -     } -     Else{ -          for(i=maxcost-1; i>=0; i--){ in             if(pro[i]!=0){//Queue non-empty -A=Pro[i]; topro[i]=0; +J + +; -k=1; the             } *         } $     }Panax Notoginseng     if(k==0) printf ("-1\n");//empty Queue output-1 -     Else{ the          for(i=0; i<rlength;i++) {//Find the current J value in the Remove queue, have the output, and K 0 +             if(rlist[i]==j) { Aprintf"%d\n", a); thek=0; +             } -         } $     }    $ } -  - intMainvoid){ the     inti; -      while(SCANF ("%d", &maxcost)! =EOF) {Wuyij=0;//the number of initialization removal is 0 thePro= (int*)malloc(maxcost*sizeof(int));//allocating the space required for an array of stored processes -Memset (Pro,0, maxcost*sizeof(int)); Wuscanf"%d",&rlength); -Rlist= (int*)malloc(rlength*sizeof(int));//allocating the space required for an array of storage lists About         if(rlist!=NULL) {  $          for(i=0; i<rlength;i++) scanf ("%d", &rlist[i]);//read in Removal ist -         } -         //Start Operation -          while(1){ Ascanf"%c", &op); scanf ("%c", &op);//Read operator +             if(op=='a'){ thescanf"%d",&i); -pro[i-1]=i; $}Else if(op=='R'){ theRe ();//Remove and output the}Else if(op=='P'){ thescanf"%d", &p);//Change Policy the}Else if(op=='e'){//Exit -                  Break; in}Else  Break; the         } theprintf"\ n"); About         //Free Space the          Free(rlist); the          Free(PRO); the     } +     return 0; -}
View Code

MANAGER POJ1281 C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.