CSP 202203-3 计算资源调度器

这道题好麻烦,主要是任务、节点、分区这些概念混在一起,比较烦人。

需要总结的有做这种模拟还是需要仔细读题,把重要条件想清楚再开始,否则会调试得比较辛苦

而且感觉代码得核心就是题目给定得判断条件得正确实现,逻辑是比较简单的。

还需要加油!!!

#include<bits/stdc++.h>
using namespace std;
using SortNode = pair<int,int>;
map<int,set<int>> AreaContainNode;//某个区有多少个节点
map<int,int> NodeAt;//节点在哪个区
map<int,set<int>> AppInNodes;//应用运行在哪些节点
map<int,set<int>> AppInAreas;
map<int,int> NodeTaskNum;//节点运行了多少个任务
int n,m;
int chooseNode(int a, int na, int pa, int paa, int paar) {
    set<int>nodes;
    if(na == 0) { for(int i = 1; i <= n; i++) {nodes.insert(i);}}
    else {nodes = AreaContainNode[na];}
    //指定可用区
    set<int>taskRequire;
    if(pa == 0) {//未指定可用应用可用区
        taskRequire = nodes;
    } else {//指定应用可用区
        set<int>tmp;
        auto& areas = AppInAreas[pa];
        for(auto& area:areas) {
            auto& nods = AreaContainNode[area];
            for(auto& ele:nods) {
                if(nodes.count(ele)) {tmp.insert(ele);}
            }
        }
        taskRequire = tmp;
    }
    if(taskRequire.empty()) {return 0;}
    //taskRequire满足前两项
    auto cover = taskRequire;
    if(paa != 0) {//如果有要求
        auto& tmp = AppInNodes[paa];
        for(auto& ele:tmp) {
            if(taskRequire.count(ele)) {
                taskRequire.erase(ele);
            }
        }
        if(taskRequire.empty()) {//如果必须满足
            if(paar) {return 0;}
        } else {
            cover = taskRequire;
        }
    }
    set<SortNode> sorter;
    for(auto& ele:cover) {
        sorter.insert({NodeTaskNum[ele],ele});
    }
    return sorter.begin()->second;
}
void setTask() {
    int f,a,na,pa,paa,paar;
    cin >> f >> a >> na >> pa >> paa >> paar;
    for(int i = 0; i < f; i++) {
        int node = chooseNode(a,na,pa,paa,paar);
        if(node){
            AppInNodes[a].insert(node);
            NodeTaskNum[node]++;
            AppInAreas[a].insert(NodeAt[node]);
        }
        cout << node << ' ';
    }
    cout << '\n';
}
void solve() {
    int g; cin >> g;
    for(int i = 0; i < g; i++) {
        setTask();
    }
}
int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        int l; cin >> l;
        AreaContainNode[l].insert(i);
        NodeAt[i] = l;
    }
    solve();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值