• MANAGER 优先队列应用


    MANAGER
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 2905   Accepted: 1027

    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

     
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    const double clf =1e-8;
    const double e= 2.718281828;
    const double PI = 3.141592653589793;
    const long int MAX=1e9;
    #define ll long long
    //const int mx=20000+1;
    priority_queue<int > p;
    priority_queue<int ,vector<int> , greater<int> > pq;
    int print[10001],cnt[10001];
    
    int main()
    {
        //freopen("data.in","r",stdin);
        //freopen("data.out","w",stdout);
        int mxn,np,minp,k,x,plen,flag;
        char c;
        while(scanf("%d",&mxn),mxn)
        {
            np=0;minp=mxn;flag=1;
            //init();
            memset(print,0,sizeof(print));
            memset(cnt,0,sizeof(cnt));
            scanf("%d",&plen);
            for(int i=0;i<plen;i++)
            {
                scanf("%d",&k); print[k]=1;
    
            }
            while(scanf("%c",&c),c!='e'){
                if(c=='a'){
                    scanf("%d",&x);cnt[x]++;
                    pq.push(x); p.push(x);
                }
                else if(c=='r'){
                    np++;bool sig=0;int ppp=0;
                    if(flag==1){
                        while(!pq.empty()){
                            ppp=pq.top();
                            if(cnt[ppp]){
                               sig=1; pq.pop();cnt[ppp]--;break;
                            } pq.pop();
                        }
                    }
                    else{
                        while(!p.empty()){
                            ppp=p.top();
                            if(cnt[ppp]){
                               sig=1; p.pop();cnt[ppp]--;break;
                            } p.pop();
                        }
                    }
                    if(print[np]){
                            if(sig)
                                printf("%d
    ",ppp);
                            else printf("-1
    ");
                    }
                }
                else if(c=='p'){
                    int c;scanf("%d",&c);
                    flag=c;
                }
            }
            printf("
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    Drupal Coder 模块远程命令执行分析(SA-CONTRIB-2016-039)
    Python 实现 ZoomEye API SDK
    程序员互动联盟第一届编码大赛第二题解题分享
    python中各进制之间的转换
    记一次ctf比赛解密题的解决(可逆加密基本破解之暴力破解)
    使用JsonConfig控制JSON lib序列化
    openMRS项目
    Harmonic Number(调和级数+欧拉常数)
    Pairs Forming LCM(素因子分解)
    Uva 11395 Sigma Function (因子和)
  • 原文地址:https://www.cnblogs.com/acmtime/p/6347889.html
Copyright © 2020-2023  润新知