求调
  • 板块灌水区
  • 楼主Jasmine_xu
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/11/17 09:22
  • 上次更新2023/11/17 12:42:47
查看原帖
求调
343452
Jasmine_xu楼主2023/11/17 09:22

第一个数据点wa了,最后一个数据点tle,求调orz

题目

/* P2278
NODE: id, ari, rt, pri, st

proces[] => storage of all proces
q_time => time that events may happen
q_proces => proces that are waiting to be run
cur_cpu => the proces in cpu now
cnt => number of cpu
*/

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int INF = -1e16;
const int maxn = 15005;

struct node{
	int id, rt, ari, pri, st;
	bool operator <(const node &w)const{
		if(pri == w.pri){
			return ari > w.ari;
		}
		return pri < w.pri;
	}
};

int cnt, cur_time, a, lst_ari;
node cur_cpu, empty_cpu, x, flg;
node proces;
queue<node>arrived_proces;
priority_queue<int, vector<int>, greater<int> >q_time;
priority_queue<node>q_proces;

void run_cpu(){
	while(!q_time.empty() && q_time.top() <= proces.ari){//sort by possible time node
		cur_time = q_time.top();//cur_time => "system" time
		while(!q_time.empty() && cur_time == q_time.top()){
			q_time.pop();
		}
		/*
		several possible event:
		1. current process in CPU is finished
		2. a new process is moved in
		*/
		if(cur_time == cur_cpu.st + cur_cpu.rt){//current process in cpu is finished
			cout << cur_cpu.id << " " << cur_time << endl;
			cur_cpu = empty_cpu;//empty_cpu.id = -1; empty_cpu.pri = 1e9; 
		}
		while(!arrived_proces.empty()){
			if(arrived_proces.front().ari > cur_time){
				break;
			}
			q_proces.push(arrived_proces.front());
			arrived_proces.pop();
		}
		
		if(!q_proces.empty()){
			//check if we need to change the current process
			x = q_proces.top();
			if(cur_cpu.id == empty_cpu.id){
				q_proces.pop();
				cur_cpu = x;
				cur_cpu.st = cur_time;
				q_time.push(cur_cpu.st + cur_cpu.rt);
			}
			else if(x.pri > cur_cpu.pri){
				q_proces.pop();
				cur_cpu.rt -= cur_time - cur_cpu.st;
				q_proces.push(cur_cpu);
				cur_cpu = x;
				cur_cpu.st = cur_time;
				q_time.push(cur_cpu.st + cur_cpu.rt);
			}
		}
	}
	return;
}

void clear_cpu(){
	while(!q_time.empty()){//sort by possible time node
		cur_time = q_time.top();//cur_time => "system" time
		while(!q_time.empty() && cur_time == q_time.top()){
			q_time.pop();
		}
		/*
		several possible event:
		1. current process in CPU is finished
		*/
		if(cur_time == cur_cpu.st + cur_cpu.rt){//current process in cpu is finished
			cout << cur_cpu.id << " " << cur_time << endl;
			cur_cpu = empty_cpu;//empty_cpu.id = -1; empty_cpu.pri = 1e9; 
		}
		if(!q_proces.empty()){
			//check if we need to change the current process
			x = q_proces.top();
			/*if(cur_time == 30){ FOR DEBUG
				while(!q_proces.empty()){
					cout << q_proces.top().id << " " << q_proces.top().ari << endl;
					q_proces.pop();
				}
				return 0;
			}*/
			if(cur_cpu.id == empty_cpu.id){
				q_proces.pop();
				cur_cpu = x;
				cur_cpu.st = cur_time;
				q_time.push(cur_cpu.st + cur_cpu.rt);
			}
			else if(x.pri > cur_cpu.pri){
				q_proces.pop();
				cur_cpu.rt -= cur_time - cur_cpu.st;
				q_proces.push(cur_cpu);
				cur_cpu = x;
				cur_cpu.st = cur_time;
				q_time.push(cur_cpu.st + cur_cpu.rt);
			}
		}
	}
	return;
}

signed main(){
	//freopen("system.in", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cnt = 0;
	empty_cpu.id = -1, empty_cpu.pri = INF;
	cur_cpu = empty_cpu;
	lst_ari = -1;
	while(cin >> a){
		if(a == -1){
			break;
		}
		proces.id = a;//No. of process
		cin >> proces.ari >> proces.rt >> proces.pri;//time for arrival; time needed for running; priority
		q_time.push(proces.ari);//priority_queue for all possible time that events can happen
		arrived_proces.push(proces);
		if(proces.ari != lst_ari){
			run_cpu();
		}
		lst_ari = proces.ari;
	}
	clear_cpu();
	return 0;
}

2023/11/17 09:22
加载中...