洛谷 - P2278 - 操作系统 - 模拟
https://www.luogu.org/problemnew/show/P2278
题目没有说同时到达的优先级最大的应该处理谁。
讲道理就是处理优先级最大的。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Process {
int id;
int itime;
int dtime;
int pri;
inline bool operator<(const Process& p)const {
if(pri==p.pri)
//大根堆,大的先出,符号反向
return itime>p.itime;
else
return pri<p.pri;
}
inline bool pri_less_than(const Process &p) {
return pri<p.pri;
}
inline void input(int &i,int &it,int &dt,int &p) {
id=i;
itime=it;
dtime=dt;
pri=p;
}
} p,cur;
priority_queue<Process> pq;
ll curtime,deltatime,pretime,nexttime;
void show_state() {
printf("ptime=%lld ctime=%lld dtime=%lld\n",pretime,curtime,deltatime);
printf(" curid=%d curdtime=%d\n",cur.id,cur.dtime);
printf(" pq.size=%d\n",(int)pq.size());
printf("\n");
}
void change_process() {
if(pq.empty())
return;
if(cur.id==0) {
//puts("next process!");
cur=pq.top();
pq.pop();
nexttime=curtime+cur.dtime;
//show_state();
} else if(cur.pri_less_than(pq.top())) {
//puts("change process!");
//命令保存当前进程并取出新进程,更新时间
pq.push(cur);
cur=pq.top();
pq.pop();
nexttime=curtime+cur.dtime;
//show_state();
}
}
void do_process() {
//puts("do process!");
if(cur.id==0)
return;
//show_state();
ll mintime=min(deltatime,(ll)cur.dtime);
deltatime-=mintime;
pretime+=mintime;
cur.dtime-=mintime;
if(cur.dtime==0) {
printf("%d %lld\n",cur.id,pretime);
cur.id=0;
nexttime=-1;
}
//show_state();
}
void update() {
do {
do_process();
//当前进程做不完了,堆顶是不是有比它大的?
change_process();
} while(cur.id&&deltatime);
//CPU正忙,尽可能从堆里面取出进程
}
int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
#endif // Yinku
int id,itime,dtime,pri;
scanf("%d%d%d%d",&id,&itime,&dtime,&pri);
curtime=itime;
pretime=itime;
deltatime=curtime-pretime;
p.input(id,itime,dtime,pri);
pq.push(p);
update();
while(~scanf("%d%d%d%d",&id,&itime,&dtime,&pri)) {
while(cur.id&&itime>nexttime) {
//puts("!");
pretime=curtime;
curtime=nexttime;
deltatime=curtime-pretime;
update();
}
//puts("?");
if(cur.id==0)
pretime=itime;
else
pretime=curtime;
curtime=itime;
deltatime=curtime-pretime;
p.input(id,itime,dtime,pri);
pq.push(p);
update();
}
pretime=curtime;
curtime=1e18;
deltatime=curtime-pretime;
update();
return 0;
}
洛谷 - P2278 - 操作系统 - 模拟的更多相关文章
- 洛谷P2278操作系统
题目 这个题是一个名副其实的考验细节和头脑清醒的一个题. 根据提议我们可以进行分类讨论. 我们用优先队列来模拟CPU,我们可以用在线的算法来写,每次输入一个进程都要判断这个进程是否可以挤掉优先队列里的 ...
- 洛谷 P5046 [Ynoi2019 模拟赛] Yuno loves sqrt technology I(分块+卡常)
洛谷题面传送门 zszz,lxl 出的 DS 都是卡常题( 首先由于此题强制在线,因此考虑分块,我们那么待查询区间 \([l,r]\) 可以很自然地被分为三个部分: 左散块 中间的整块 右散块 那么这 ...
- 洛谷P2278 [HNOI2003] 操作系统
题目传送门 分析:题目中提到了优先级,很显然这题要用优先队列+模拟.题目中很多细节需要注意,还是在代码中解释吧,这里我用的是手打的堆. Code: #include<bits/stdc++.h& ...
- 洛谷 P2278 [HNOI2003]操作系统
题目传送门 解题思路: 一道没啥思维含量的模拟题,但是个人感觉代码实现不简单,可能是我太弱了,花了我6个小时,3次重写. AC代码: #include<iostream> #include ...
- AC日记——导弹拦截 洛谷 P1020 (dp+模拟)
题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...
- AC日记——潜伏者 洛谷 P1071 (模拟)
题目描述 R 国和 S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的 R 国间谍小 C 终于摸清了 S 国军用密码的编码规则: 1. S 国军方内部欲发送的原 ...
- 洛谷P3952 时间复杂度(模拟)
题意 题目链接 Sol 咕了一年的题解..就是个模拟吧 考场上写的递归也是醉了... 感觉一年自己进步了不少啊..面向数据编程的能力提高了不少 #include<bits/stdc++.h> ...
- 洛谷 - P3952 - 时间复杂度 - 模拟
https://www.luogu.org/problemnew/show/P3952 这个模拟,注意每次进入循环的时候把新状态全部入栈,退出循环的时候就退栈. 第一次就错在发现ERR退出太及时,把剩 ...
- 洛谷 P1071 潜伏者 —— 模拟
题目:https://www.luogu.org/problemnew/show/P1071 按题意模拟即可. 代码如下: #include<iostream> #include<c ...
随机推荐
- C++输入一行字符串的一点小结
C++输入一行字符串的一点小结 原文链接: http://www.wutianqi.com/?p=1181 大家在学习C++编程时.一般在输入方面都是使用的cin. 而cin是使用空白(空格,制表符和 ...
- TXT文本写入数据库
load data local infile "D:/abc.txt" into table lee; leedabao.txt内容如下,中间用Tab隔开: 2 yuanpeng ...
- Leetcode 001-twosum
#Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
- 一起来学linux:磁盘与文件系统:
对于文件系统来说,windows上最长用的就是FAT32和NTFS.在Linux上时候用的是Ext2.在linux中,文件权限与文件属性这两部分会被存储在不同的块,权限与权限放置到inode中,实际数 ...
- 20170319 ABAP 生成XML文件
方法一:ABAP 使用method方式操作XML 转自:http://www.cnblogs.com/jiangzhengjun/p/4265595.html 方法二:STRANS 转换工具;使用st ...
- beego 导入一个普通的包都会执行init方法,如果是struct就不会执行
default.go package controllers import ( "beego-test/models" "beego-test/service" ...
- C++模板(二)【转】
本文转自:http://www.cnblogs.com/gw811/archive/2012/10/25/2738929.html C++模板 模板是C++支持参数化多态的工具,使用模板可以使用户为类 ...
- POJ2389 —— 高精度乘法
直接上代码了: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib& ...
- codeforces B. George and Round 解题报告
题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...
- Notepad++安装xml插件
环境: win7 64位 Notepad++7.3.3 原生的Notepad++不自带xml文件的插件,所以在显示xml文件时并不分行(如下图所示),对于用户编辑,查看的操作而言,并不友好,所以需要安 ...