MANAGER
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

题目大意:

线程模拟。

ax——将一个花费为x的进程加到队列中

r——假设可能。依照当前管理者的策略,删除一个进程

p i ——运行管理者的策略i。当中i是1或者2。缺省值为1

e——请求列表终止

两个管理者的策略为:

1——删除最小耗费进程

2——删除最大耗费进程

输出指定的删除序列

#include <iostream>
#include<algorithm>
using namespace std;
int cmp1(int a,int b)
{
return a>b;
}
int cmp2(int a,int b)
{
return a<b;
}
int main()
{ int num;
while(cin>>num&&num)
{
int p=1;
int n;
cin>>n;
int a[1010]={0},b[1010]={0},c[2010]={0};
int i;
for(i=1;i<=n;i++)
cin>>b[i];
int a1=1,b1=1,c1=1;
char ch;
i=0;
while(cin>>ch&&ch!='e')
{
if(ch=='a')
{
cin>>a[a1];
a1++;
}
if(ch=='p')
cin>>p;
if(ch=='r')
{
if(p==1)//删除最小进程
{
sort(a+1,a+a1,cmp1);
c[c1]=a[a1-1];
c1++;
a1=a1-1;
}
if(p==2)//删除最大进程
{
sort(a+1,a+a1,cmp2);
c[c1]=a[a1-1];
c1++;
a1=a1-1;
}
}
}
for(i=1;i<=n;i++)
cout<<c[b[i]]<<endl;
cout<<endl;
}
return 0;
} /*
5
2
1 3
a 2
a 3
r
a 4
p 2
r
a 5
r
e
*/

刚開始提交WrongAnswer 后来注意到时sort函数的使用,数组開始下标从0開始还是从1開始sort括号中的的列表不同,

sort(a+1,a+a1,cmp1);

我的下标从1開始。

sort函数详情见http://blog.csdn.net/sunshumin/article/details/37756027

再提交时是PE错误,改成一次while循环加一个空行。ac。

POJ 1281 MANAGER的更多相关文章

  1. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  2. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  3. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  4. poj 题目分类(1)

    poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...

  5. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  6. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  7. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  8. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  9. [POJ题目分类][转]

    Hint:补补基础... 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治 ...

随机推荐

  1. [HTML5] aria-label & aria-labelledby

    'aria-labelledby' overwrite 'aria-label' overwirte native element label. TOP-LEFT: aria-label overwr ...

  2. Android APP漏洞挖掘

    0x00 1.组件公开安全漏洞 參考Android 组件安全. 2.Content Provider文件文件夹遍历漏洞 參考Content Provider文件文件夹遍历漏洞浅析. 3.Android ...

  3. Struts2概述及与Struts1的对照

    Struts2 概述 1,仍然是一个基于请求响应的MVC框架 2,Struts2不是Struts1的升级 3,Struts2与Struts1的体系结构差距非常大 4,Struts2採用了还有一个MVC ...

  4. JSTL中的常用EL函数(fn:contains(str,subStr))

    转自:https://blog.csdn.net/u012843873/article/details/53289238 ① fn:toLowerCase ④fn:length fn:length函数 ...

  5. 图片词典 Picture Dictionary

    图片词典/可视词典 Picture Dictionary 某些 APP 又有新功能可以加入了.

  6. Temporary Tables临时表

    1简介 ORACLE数据库除了可以保存永久表外,还可以建立临时表temporary tables.这些临时表用来保存一个会话SESSION的数据, 或者保存在一个事务中需要的数据.当会话退出或者用户提 ...

  7. RedHat/CentOS(Linux)双网卡bond(mode=6)

    1. 将/etc/sysconfig/network-scripts/ifcfg-ens1f0和ifcfg-ens1f1文件备份到root目录下2. 修改/etc/sysconfig/network- ...

  8. Unity 需不需要再建Assets文件夹

    不需要,默认所有文件都是在Assets文件夹下创建的,看不到是因为设置了单栏模式,开启双栏模式就能看到了.

  9. ZBrush软件Texture纹理调控板

    在zbrush4r8中对一个模型进行纹理制作在速度和易用性方面有诸多优势,通过Texture调控板创建.导入和输出纹理是及其方便且快捷的. Import (导入):导入Photoshop (.psd) ...

  10. 粘包解决高端_Server

    from socket import * #导入套接字模块的所有命令import subprocess #导入subprocess模块,用于执行命令行import struct #导入struck模块 ...