CSP认证-计算资源调度器

目录

  • T3-计算资源调度器

T3-计算资源调度器

在这里插入图片描述


#include 
using namespace std;
const int N = 1010, M = 2010;
int n, m, l[N], busy[N], g; // busy: 节点运行任务数
vector<int> zone2node[N];
map<int, set<int>> app2node; // 应用2节点
map<int, vector<int>> app2zone; // 应用2区域
vector<int> all_zone;

int main()
{
    scanf("%d%d", &n, &m);
    // O(NlogN)
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &l[i]);
        zone2node[l[i]].push_back(i);
        all_zone.push_back(l[i]);
    }
    scanf("%d", &g);
    int f, a, na, pa, paa, paar;
    for(int i = 1; i <= g; i++)
    {
        scanf("%d%d%d%d%d%d", &f, &a, &na, &pa, &paa, &paar);
        for(int j = 1; j <= f; j++)
        {
            vector<int> target, backup, zone;
            //记录:app2zone
            if(na != 0){
                target = zone2node[na];
                if(target.empty()){
                    printf("0 ");
                    continue;
                }
                zone.push_back(na);
            } 

            if(pa != 0 && app2zone.count(pa))
            {
                vector<int> v = app2zone[pa];
                if(v.empty()) {
                    printf("0 ");
                    continue;
                }

                if(na != 0)
                {
                    bool flag = false;
                    for (auto z:v)
                        if(z == na){
                            flag = true;
                            break;
                        } 
                    if(!flag) {
                        printf("0 ");
                        continue;
                    } 
                }
                // na == 0
                else{
                    zone = v; 
                }
            }

            else if(pa != 0 && !app2zone.count(pa))
            {
                printf("0 ");
                continue;
            }

            //选择此时运行计算任务数量最少的计算节点;
            //选择编号最小的计算节点。

            int bsy = 1e9, ans_node = 1e9;
            set<int> forbid_node = app2node[paa];
            
            // 考虑反亲和性
            if(na == 0 && pa == 0) zone = all_zone;
            for(auto z:zone)
            {
                vector<int> nodes = zone2node[z];
                for(auto nn:nodes)
                {
                    if(forbid_node.count(nn)) continue;
                    if(bsy > busy[nn] || (bsy == busy[nn] && ans_node > nn)){
                        bsy = busy[nn];
                        ans_node = nn;
                    }
                }
            }
            
            // 是否有必要不考虑反亲和性
            if(ans_node == 1e9 && paar != 1)
            {
                bsy = 1e9, ans_node = 1e9;
                for(auto z:zone)
                {
                    vector<int> nodes = zone2node[z];
                    for(auto nn:nodes)
                    {
                        if(bsy > busy[nn] || (bsy == busy[nn] && ans_node > nn)){
                            bsy = busy[nn];
                            ans_node = nn;
                        }
                    }
                }
            }

            if(ans_node == 1e9)
            {
                printf("0 ");
                continue;
            }
            int ans_zone = l[ans_node];
            // 记录app2zone, app2node, busy
            app2zone[a].push_back(ans_zone);
            app2node[a].insert(ans_node);
            busy[ans_node]++;
            printf("%d ", ans_node);
            
        }
        printf("\n");
    }
    return 0;
}

博主原创,转载请注明出处

你可能感兴趣的:(算法竞赛笔记,算法,数据结构,c++)