zoukankan      html  css  js  c++  java
  • J

    J - MANAGER(2.4.5)
    Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

    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
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAXn = 10010;
    
    bool cmp(int a, int b)
    {
        return a < b;
    }
    
    int main()
    {
        int n, m, i, j, k, l, p, rmovList[MAXn], curList[MAXn], rmovNum[MAXn];
        char cmd;
        while (cin >> n >> m)
        {
            for (i = 0; i < m; i++)
                cin >> rmovNum[i];
            memset(curList, 0, sizeof(curList));
            p = 1;
            j = 1;
            k = 1;
            while (1)
            {
                cin >> cmd;
                if (cmd == 'e')
                    break;
                else if (cmd == 'a')
                {
                    cin >> curList[j];
                    sort(curList + 1, curList + j + 1, cmp);
                    j++;
                    continue;
                }
                else if (cmd == 'r')
                {
                    if (p == 1)
                    {
                        rmovList[k] = curList[1];
                        for (l = 2; l < j; l++)
                            curList[l-1] = curList[l];
                        k++;
                        j--;
                        continue;
                    }
                    else if (p == 2)
                    {
                        rmovList[k] = curList[j-1];
                        j--;
                        k++;
                        continue;
                    }
                }
                else if (cmd == 'p')
                    cin >> p;
            }
            for (i = 0; i < m; i++)
            {
                if(rmovNum[i] > k - 1)
                    cout << -1 << endl;
                else
                    cout << rmovList[rmovNum[i]] << endl;
            }
            cout << endl;
        }
    
        return 0;
    }
    


    
    
  • 相关阅读:
    Python Day 30 网络编程 (串讲 FTP作业)
    Python Day 29 网络编程 ( socket中的一些常见方法,登录验证,socketserver模块)
    Python Day 28 网络编程 (socket远程命令执行, tcp黏包现象,以及struck模块的使用 )
    Python Day 27 网络编程 (socket udp)
    Python Day 26 网络编程 ( 网络编程基础 socket tcp)
    Python Day 25 蜥蜴串讲
    Python Day 24 面向对象进阶(双下方法 __new__ __del__ item系列 异常处理)
    Python Day 23 面向对象进阶(内置方法:反射,isinstance和issubclass,__str__和__repr__和其他双下方法)
    Python Day 21 面向对象 (面向对象的三大特性(二)继承,多态,封装,几个装饰器函数)
    aaencode:用颜文字来加密吧
  • 原文地址:https://www.cnblogs.com/u013533289/p/4477304.html
Copyright © 2011-2022 走看看