POJ #2448 A New Operating System
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 1165 | Accepted: 110 | |
Case Time Limit: 5000MS |
Description
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
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
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
Source
POJ上又一个通过率较低的模拟题,还有一个是POJ 1025 Department。
CreateProcess(PID,Memory,Priority)
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的更多相关文章
- 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 ...
- The World's Only Advanced Operating System
The World's Only Advanced Operating System
- Unable to open the physical file xxxx. Operating system error 2
在新UAT服务器上,需要将tempdb放置在SSD(固态硬盘)上.由于SSD(固态硬盘)特性,所以tempdb的文件只能放置在D盘下面,而不能是D盘下的某一个目录下面. ALTER DATABASE ...
- CREATE FILE encountered operating system error 5(Access is denied.)
这篇博文主要演示"CREATE FILE encountered operating system error 5(Access is denied.)"错误如出现的原因(当然只是 ...
- Linux启动报错missing operating system
用UltraISO制作了一个Red Hat Enterprise Linux Server release 5.7系统的U盘启动盘,然后在一台PC上安装,由于安装过程中在干别的事情,有些选项没有细看. ...
- Learning Roadmap of Robotic Operating System (ROS)
ROS Wiki: http://wiki.ros.org/ Robots Using ROS Textbooks: A Gentle Introduction to ROS Learning ROS ...
- 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 ...
- Multiprocessor Operating System Design Considerations SYMMETRIC MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION An SMP operating syst ...
- u盘安装CENTOS后,启动missing operating system ,只能用U盘才能启动系统
好久之前就想把家里闲置的那台老的不能再老的笔记本换成linux的,用来学习 从N久之前用光盘安装的时候发现光驱坏掉了之后就没有再装过,最近又想安装于是就试了U盘安装 U盘安装过程也很简单,只需要制作一 ...
随机推荐
- Oracle 常用操作【02】数据库特性
1. 导出 oracle 注释 -- 表明細+表注释+字段明细+字段注释 a.一个用户下的表明細+表注释+字段明细+字段注释 select ATC.OWNER, atC.TABLE_NAME, utc ...
- 网游中的网络编程系列1:UDP vs. TCP
原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 目录 网游中的网络编程系列1:UDP vs. TCP 网游中的网络编程2:发送和接收数据包 网游中的网 ...
- 创业这三年¥.NET之尴尬处境
创业这三年#迈出第一步 创业这三年@各种奇遇 之前写的文章有兴趣的大家可以看看. 本来没有打算写这样一篇会遭人拍砖的文章,但是发现大家每天忙于编码,对市场环境..Net生态没有一个真实.多角度的认识, ...
- ASP.NET MVC3入门教程之第一个WEB应用程序
本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=91&extra=page%3D1 上一节,我们已经搭建好了AS ...
- HTC Vive 与Leap Motion 出现位置错误的问题
Leap Motion已经支持VR, 但是官方没有支持HTC Vive的例子. 按照官方的文档, 其实是有问题的: https://developer.leapmotion.com/documenta ...
- pay-as-you-go
What is pay as you go? A pay as you go deal means you aren’t tied into a contract and can top up you ...
- RHCE认证考试教材
前段时间考RHCE7,顺便给大家分享下RHCE6.7的中文教材!毕竟此书是官方的培训教材,还是值得看看!RHEL6.7承前启后的,给个赞! 下载:http://pan.baidu.com/s/1nu9 ...
- 使用delegate实现简单的查询功能
protected void imgbtnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e) { string keyWo ...
- 十天冲刺---Day5
站立式会议 站立式会议内容总结: 燃尽图 照片 PM确实不应该交给组内编码最强的人来做. 编码的过程还要考虑整个项目的流程压力较大. 需要队友的支持和沟通.
- 第八章:Java集合
1.Java集合 A:对象的容器. B:实现数据结构(栈.队列) 2. Set:无序不重复 List: 有序可重复,长度可变. Map: 存放键值对. 3. Iterator foreach