CSP202203-3计算资源调度器

CSP202203-3计算资源调度器

题目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

数据结构图解

在这里插入图片描述

代码

#include<bits/stdc++.h>
using namespace std;
typedef struct node{
	int id;//编号 
	int block;//区
	int nums;//当前的任务数 
	set<int> task; //所运行任务的编号 
	
	bool operator < (const node &a)const{
		if(this->nums!=a.nums){
			return this->nums<a.nums;
		}
		else {
			return this->id<a.id;
		}
	}
}node;

unordered_map<int,node> Node;

//编号为n的可用区内的计算节点
void fax1(int n,vector<int> &temp){
	for(auto it=temp.begin();it!=temp.end();){
		if(Node[*it].block!=n){
			temp.erase(it);
		}
		else it++;
	}
	return;
}

//和编号为n的应用的计算任务在同一个可用区
void fax2(int n,vector<int> &temp){
	set<int> qu; 
	for(auto it=Node.begin();it!=Node.end();it++){
		if(it->second.task.count(n)==1){
			qu.insert(it->second.block);
		}
	}
	for(auto it=temp.begin();it!=temp.end();){
		if(qu.count(Node[*it].block)==0){
			temp.erase(it);
		}
		else it++;
	}
	return;
}

//表示不能和编号为n的应用的计算任务在同一个计算节点上运行
vector<int> fax3(int n,vector<int> temp){
	for(auto it=temp.begin();it!=temp.end();){
		if(Node[*it].task.count(n)==1){
			temp.erase(it);
		}
		else it++;
	}
	return temp;
}

int main(){
#ifdef inline
	freopen("stdin.txt","r",stdin);
#endif
	ios::sync_with_stdio(false);
	int n,m;cin>>n>>m; //n个节点,m个区
	int i=1;
	
	vector<int> idsum;
	while(i<=n){
		int temp;
		cin>>temp;
		Node.insert(pair<int,node>(i,node{i,temp,0,set<int>()}));
		idsum.push_back(i);
		i++;
	}
	int T;cin>>T;
	while(T--){
		int a1,a2,a3,a4,a5,a6;
		cin>>a1>>a2>>a3>>a4>>a5>>a6;
		
// #ifdef inline
// 	for(auto &i:temp){
// 		cout<<i<<" ";
// 	}
// 	cout<<"*************"<<endl;
// #endif
		
		while(a1--){
			vector<int> temp=idsum,temp2;
			//计算节点亲和性
			if(a3!=0) {
				fax1(a3,temp);
			}
			//计算任务亲和性
			if(a4!=0) {
				fax2(a4,temp);
			}
			//计算任务反亲和性
			if(a5!=0) {
				temp2=fax3(a5,temp);
			}
			//计算任务反亲和性要求是必须满足的且无可用节点   或 不考虑 计算任务反亲和性也无可用节点 
			if((temp2.empty()&&a6==1)||temp.empty()) {
				cout<<0<<" ";
				continue;
			}
			//计算任务反亲和性要求是尽量满足的 且 无可用节点   则 不考虑 计算任务反亲和性 
			else if(temp2.empty()&&a6==0){
				temp2=temp;
			}
			//排序 按照节点的已有的作业数、序号 
			sort(temp2.begin(),temp2.end(),[](const auto &a,const auto &b)->bool{
				return Node[a]<Node[b];
			});
			//更新节点信息 
			Node[temp2[0]].nums++;
			Node[temp2[0]].task.insert(a2);
			cout<<Node[temp2[0]].id<<" ";
		}
		cout<<endl;
		
	}
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值