Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 1165   Accepted: 110
Case Time Limit: 5000MS

Description

May is a lovely girl. Due to her filial piety, she wants to give a present on her mother's birthday. Because both her parents are the top programmer in the world, she decided to design a new program, a special program that is an Operating System! With the help of her excellent programming skill, May has already finished the kernel of the new OS. And the birthday is coming, she is afraid that time is not enough to finish the entire project in time. As her best net-pal, it's your duty to help her.
This is a multitask OS, processes run at the same time. There are following command in the OS:

CreateProcess(PID,Memory,Priority)

A new process is created with the process identification PID and
memory size, and a priority. The priority in this command is called
outer priority of a process.

AddMessage(PID,Priority)

That means, add a new message to the message queue of process PID,
with the priority of Priority. The message with higher Priority will run
earlier that lower ones. The Priority is called inner priority.

Run

Find out the message with biggest HP. HP is defined as the product
of the inner priority of a message and the corresponding process
priority. If two or more messages have the same HP, the one with
smallest PID will run first. Print the information "Run: HP" to the
output file, HP will be replaced by the message HP you found, or print
"Empty" instead if the message queue is empty. Finally remove this
message if exist.

ChangePriority(PID,NewValue)

Change the outer priority of process PID to NewValue.

GetMemory(PID,Memory)

Process PID memory increases the amount of Memory.

FreeMemory(PID,Memory)

Process PID memory decreases the amount of Memory.

RunProcess(PID)

Similar with Run command. Find out the message in the process PID
message queue, print the information "Run Process: Priority" to the
output file, Priority will be replaced by the message priority you
found, or print "Empty" instead if the message queue is empty. Finally
remove this message if exist.

CloseMaxMemory

Find out the process that used the largest number of memory and
close it if exists (if tie, the one with smallest PID should be release
first), or print "Empty" instead.

CloseProcess(PID)

Close the process with the identification of PID.

Whenever a process' memory is less than or equal to 0, it will be
close automatically. In any of the above commands except the first one,
if the PID doesn't exist, please print an "Error" to the output. For the
first command, if the PID is already exist, print an "Error" and ignore
this command.

Input

First
line in the input is an integer number N (1 <= N <= 100000),
which represents the number of commands. The next N lines, each gives a
command described above. Any number given in the input file will be
non-negative integer and will not be more than 1000000000.

Output

The output format has been described above.

Sample Input

11
CloseMaxMemory
CreateProcess(1,100,1)
CreateProcess(2,200,1)
CreateProcess(3,300,1)
AddMessage(1,9)
AddMessage(2,19)
AddMessage(1,10)
GetMemory(2,999)
CloseMaxMemory
Run
RunProcess(1)

Sample Output

Empty
Run: 10
Run Process: 9

Hint

The total size of the input files may be as large as 21.8MB.

Source


Solution
POJ上又一个通过率较低的模拟题,还有一个是POJ 1025 Department
这道题比1025简单了不少,但仍有些难度。
比较难处理的操作就是Run和CloseMaxMemory,而这两个操作又比较相似。
不论简单复杂,模拟题的一般思路是分析事件(events)。这个题目对要求维护的东西叙述比较明确,所涉及的事件基本上就是所列的若干操作。
实现思路:
map<int,pair<int,int>> info;维护process的信息
对每个process,用一个priority_queue<int> que[N]; 维护其message队列,这要求将pid离散化,用map<int,int> id;实现。
用两个set<pair<int,long long>> memory, hp; 分别支持CloseMaxMemory、Run两操作
 
难点:
如何实时更新上述4个数据结构。
 
下面具体叙述如何实现各个操作:
 
CreateProcess(PID,Memory,Priority)
更新infoidmemory,初始化que[id[PID]]
 
AddMessage(PID,Priority)
更新que[id[pid]]hp
 
Run
更新hp,对应的message队列。
 
ChangePriority(PID,NewValue)
更新info[PID],更新hp
 
GetMemory(PID,Memory)
更新memory
 
FreeMemory(PID,Memory)
更新memory,可能涉及清除一个process。
 
RunProcess(PID)
更新que[id[PID]]hp
CloseMaxMemory
更新hp, memory,info;清空对应的message队列。 
 
CloseProcess(PID)
更新hpmemory,info;清空对应的message队列。 
 
注意:

CreatProcess(PID, Memory, Priority)操作中Memory可能为零(Any number given in the input file will be non-negative integer)。


Implementation:

 #include <cstdio>
#include <set>
#include <map>
#include <queue>
#define LL long long
#define MEM second
#define PRO first
#define PID first
#define HP second
using namespace std; const int N(1e5+);
priority_queue<int> que[N]; typedef pair<int,LL> P; map<int,P> mp;
map<int,int> ID; bool cmp(const P &a, const P &b){
return a.second!=b.second?a.second>b.second:a.first<b.first;
} set<P, bool(*)(const P &, const P &)> o(cmp), m(cmp); void Erase(int pid){
int id=ID[pid];
if(que[id].size()){
o.erase(P(pid, (LL)que[id].top()*mp[pid].PRO));
que[id].pop();
}
} void remove_process(int pid){
Erase(pid);
m.erase(P(pid, mp[pid].MEM));
int id=ID[pid];
for(; que[id].size(); que[id].pop());
mp.erase(pid);
} void Insert(int pid){
int id=ID[pid];
if(que[id].size()){
o.insert(P(pid, (LL)que[id].top()*mp[pid].PRO));
}
} void close_process(int pid){
if(mp.find(pid)==mp.end()) puts("Error");
else remove_process(pid);
} void close_max_memory(){
if(m.empty()) puts("Empty");
else remove_process(m.begin()->PID); //error-prone
} void run_process(int pid){
if(mp.find(pid)==mp.end()){
puts("Error");
return;
}
int id=ID[pid];
if(que[id].empty()) puts("Empty");
else{
printf("Run Process: %d\n", que[id].top());
Erase(pid), Insert(pid);
}
} void modify_memory(int pid, int mem){
if(mp.find(pid)==mp.end()){
puts("Error");
return;
}
if(mp[pid].MEM+mem<=)
remove_process(pid);
else{
P now=mp[pid];
m.erase(P(pid, now.MEM));
m.insert(P(pid, now.MEM+mem));
mp[pid].MEM+=mem;
}
} void change_priority(int pid, int pro){
if(mp.find(pid)==mp.end()){
puts("Error");
return;
}
int id=ID[pid];
if(que[id].size()){
int i_pro=que[id].top();
o.erase(P(pid, (LL)mp[pid].PRO*i_pro));
o.insert(P(pid, (LL)pro*i_pro));
}
mp[pid].PRO=pro;
} void run(){
if(o.empty()) puts("Empty");
else{
printf("Run: %lld\n", o.begin()->HP);
int pid=o.begin()->PID;
Erase(pid), Insert(pid);
}
} void add_message(int pid, int pro){
if(mp.find(pid)==mp.end()){
puts("Error");
return;
}
int id=ID[pid];
if(que[id].size()){
int i_pro=que[id].top();
o.erase(P(pid, (LL)i_pro*mp[pid].PRO));
}
que[id].push(pro);
o.insert(P(pid, (LL)que[id].top()*mp[pid].PRO));
} int tail; void create_process(int pid, int mem, int pro){
if(mp.find(pid)!=mp.end()){
puts("Error");
return;
}
if(mem){
mp[pid]=P(pro, mem);
m.insert(P(pid, mem));
ID[pid]=tail++;
}
} char s[]; int main(){
int n, pid, mem, pro;
for(scanf("%d", &n); n--; ){
scanf("%s", s);
if(s[]=='C'){
if(s[]=='r'){
sscanf(s, "CreateProcess(%d,%d,%d)", &pid, &mem, &pro);
create_process(pid, mem, pro);
}
else if(s[]=='l'){
if(s[]=='M')
close_max_memory();
else{
sscanf(s, "CloseProcess(%d)", &pid);
close_process(pid);
}
}
else{
sscanf(s, "ChangePriority(%d,%d)", &pid, &pro);
change_priority(pid, pro);
}
}
else if(s[]=='A'){
sscanf(s, "AddMessage(%d,%d)", &pid, &pro);
add_message(pid, pro);
}
else if(s[]=='G'){
sscanf(s, "GetMemory(%d,%d)", &pid, &mem);
modify_memory(pid, mem);
}
else if(s[]=='F'){
sscanf(s, "FreeMemory(%d,%d)", &pid, &mem);
modify_memory(pid, -mem);
}
else{
if(s[]){
sscanf(s, "RunProcess(%d)", &pid);
run_process(pid);
}
else run();
}
}
return ;
}
 
 

POJ #2448 A New Operating System的更多相关文章

  1. DBCC CHECKDB 遭遇Operating system error 112(failed to retrieve text for this error. Reason: 15105) encountered

    我们一个SQL Server服务器在执行YourSQLDBa的作业YourSQLDba_FullBackups_And_Maintenance时遇到了错误: Exec YourSQLDba.Maint ...

  2. The World's Only Advanced Operating System

    The World's Only Advanced Operating System

  3. Unable to open the physical file xxxx. Operating system error 2

    在新UAT服务器上,需要将tempdb放置在SSD(固态硬盘)上.由于SSD(固态硬盘)特性,所以tempdb的文件只能放置在D盘下面,而不能是D盘下的某一个目录下面. ALTER  DATABASE ...

  4. CREATE FILE encountered operating system error 5(Access is denied.)

    这篇博文主要演示"CREATE FILE encountered operating system error 5(Access is denied.)"错误如出现的原因(当然只是 ...

  5. Linux启动报错missing operating system

    用UltraISO制作了一个Red Hat Enterprise Linux Server release 5.7系统的U盘启动盘,然后在一台PC上安装,由于安装过程中在干别的事情,有些选项没有细看. ...

  6. Learning Roadmap of Robotic Operating System (ROS)

    ROS Wiki: http://wiki.ros.org/ Robots Using ROS Textbooks: A Gentle Introduction to ROS Learning ROS ...

  7. Full exploitation of a cluster hardware configuration requires some enhancements to a single-system operating system.

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Operating System Desi ...

  8. Multiprocessor Operating System Design Considerations SYMMETRIC MULTIPROCESSORS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION An SMP operating syst ...

  9. u盘安装CENTOS后,启动missing operating system ,只能用U盘才能启动系统

    好久之前就想把家里闲置的那台老的不能再老的笔记本换成linux的,用来学习 从N久之前用光盘安装的时候发现光驱坏掉了之后就没有再装过,最近又想安装于是就试了U盘安装 U盘安装过程也很简单,只需要制作一 ...

随机推荐

  1. swift 初探NSURLSession

    进行封装, 新建一个类.network class Network1: NSObject { // 没有参数+结果的get  自定义 HTTP method 和 URL+闭包 static func ...

  2. C 语言学习的第 02 课:C 语言的开发环境

    工欲善其事,必先利其器.不知道还是不是记得上一篇文章中说到的,计算机本身是一个数据输入及输出的设备.所以,为了将你大脑中的各种 idea 输入到电脑,且最终生成能够执行的程序,总是要预备点什么的. 通 ...

  3. 40-cut 简明笔记

    从输入行中选取字符或者字段 cut [options] [file-list] cut 从输入行中选取字符或者字段,并将他们写到标准输出,字符和字段从1开始编号 参数 file-list 是文件的路径 ...

  4. Collection中list集合的应用常见的方法

    集合 : 用存放对象的容器(集合)     Collection : 跟接口 : 单列集合          ---> List :有序的 ,元素是可以重复的.          ---> ...

  5. RAP在centos上的部署

    在centos7上部署RAP(非官方) 作者批注:该部署文档为网友贡献,仅供参考.war请参考主页README.md下载最新版本哟~~~ 感谢热情网友的Wiki整理!万分感谢! 系统: centos7 ...

  6. oracle-day1

    今天的学习内容是oracle产品的三种安装方式,还有使用dbca静默建库 oracle产品的三种安装方式分别为: 1.图形化(Java向导)安装引导 2.使用应答文件静默安装 3.直接将装好的orac ...

  7. git 最常用命令

    新建分支 git branch a #分支名称为a 切换到develop分支 git checkout a 新建分支并切换 git checkout -b a 推送到远程分支 git push ori ...

  8. 【URAL 1486】Equal Squares

    题意:求给定字符矩阵中相同正方形矩阵的最大边长和这两个相同正方形的位置 第一次写字符串哈希,选两个不同的模数进行二维字符串哈希. 本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到<Hash在 ...

  9. 状态压缩 poj 3254

    n * m 个玉米 n*m个数字 0 或者1 1可以种玉米 0 不能  种玉米不能相邻 计算有几种 种的方法 #include<stdio.h> #include<algorithm ...

  10. 小 div在大 div中左右上下居中

    <!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content=& ...