• MANAGER POJ1281 C语言


    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

    我不管怎么调都不能在UVALive、ZOJ上通过,UVALive是WA,ZOJ是PE,我估计是输出格式的问题,但我已经用完我的想象力了实在不知道该怎么输出,英语也不怎么好,不怎么读的懂。

    这道题就是模拟,因为不知道会有多少个数据输进来,所以我存储移除列表和进程的数组都是用的动态分配内存,用完后在释放,然后在重新申请。存储进程的数组的大小可以根据进程的最大开销来分配,存储移除列表的数组根据题目给的数分配。

     1 //removal list是输出第i个被移除的进程
     2 //如1、3,输出第1个和第3个被移除的进程 
     3 #include <stdio.h>
     4 #include <malloc.h>
     5 #include <string.h>
     6 
     7 int maxcost;//the maximum cost of the processes
     8 int p=1;//policy
     9 int rlength;//the length of the process
    10 int *rlist=NULL;//用来存储移除列表的数据
    11 char op;//操作
    12 int *pro=NULL;//进程数组
    13 int j,k=0;//j为第几次移除,k为是否成功移除的标志,0为否,1为是 
    14 
    15 void re(){
    16     int i,a;
    17     if(p==1){
    18         for(i=0;i<maxcost;i++){
    19             if(pro[i]!=0){//队列非空
    20                 a=pro[i];
    21                 pro[i]=0;
    22                 j++;
    23                 k=1;
    24                 break; 
    25             }
    26         }
    27     }
    28     else{
    29         for(i=maxcost-1;i>=0;i--){
    30             if(pro[i]!=0){//队列非空
    31                 a=pro[i];
    32                 pro[i]=0;
    33                 j++;
    34                 k=1;
    35                 break;
    36             }
    37         }
    38     }
    39     if(k==0) printf("-1
    ");//空队列输出-1
    40     else{
    41         for(i=0;i<rlength;i++){//在移除队列中查找有无当前的j值,有则输出,并将k置0 
    42             if(rlist[i]==j){
    43                 printf("%d
    ",a);
    44                 k=0;
    45             }
    46         }
    47     }   
    48 }
    49 
    50 int main(void){
    51     int i;
    52     while(scanf("%d",&maxcost)!=EOF){
    53         j=0;//初始化移除次数为0 
    54         pro=(int*)malloc(maxcost*sizeof(int));//分配存储进程的数组需要的空间 
    55         memset(pro,0,maxcost*sizeof(int));
    56         scanf("%d",&rlength);
    57         rlist=(int*)malloc(rlength*sizeof(int));//分配存储列表的数组需要的空间
    58         if(rlist!=NULL){ 
    59         for(i=0;i<rlength;i++) scanf("%d",&rlist[i]);//读入removal ist
    60         }
    61         //开始操作
    62         while(1){
    63             scanf("%c",&op);scanf("%c",&op);//读取操作符
    64             if(op=='a'){
    65                 scanf("%d",&i);
    66                 pro[i-1]=i;
    67             }else if(op=='r'){
    68                 re();//移除并输出
    69             }else if(op=='p'){
    70                 scanf("%d",&p);//更换policy 
    71             }else if(op=='e'){//退出 
    72                 break;
    73             }else break;
    74         }
    75         printf("
    ");
    76         //释放空间 
    77         free(rlist);
    78         free(pro);
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    SQL语句中的GO
    别人如何使用JsUnit的
    类,子类,继承,virtual, abstract, override
    尝到JsUnit自动测试的甜头
    A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误
    正则表达式收集
    jquery 无限级下拉菜单
    javascript学习笔记,绝对值,倒计时,按数字计算
    java 常识
    jquery ajax 从后台获取数据时 firefox控制台显示未组织好错误
  • 原文地址:https://www.cnblogs.com/20174317zhuyuan/p/9416704.html
Copyright © 2020-2023  润新知