2021华为软件精英挑战赛

Posted WILLPOWER-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021华为软件精英挑战赛相关的知识,希望对你有一定的参考价值。


47

code

CodeCraft-2021.cpp

#include "define.h"
#define TEST
// #define UP
// #define RUN
#ifdef TEST
const string filePath = "./training-1.txt";
#endif

//服务器类型的存储
unordered_map<string, Server_Type> serType;
//当前运行的服务器
vector<Server> onServers;
//需要购买的服务器
unordered_map<string, int> serToBuy;
//虚拟机
unordered_map<string, Vm_Type> vmType;
//将虚拟机的id同服务器id以及服务器的节点对应起来
unordered_map<int, Id_Map> vmIdInfo;
//一天的增加请求存储
vector<Add_Requst> addRequests;
//一天中双节点增加的请求
vector<Add_Requst> twoNodeReq;
//一天中单节点增加的请求
vector<Add_Requst> noeNodeReq;
//迁移请求
vector<Add_Requst> migRequests;
//一天的删除请求存储
vector<Del_Requst> delRequests; 
//所有请求
vector<All_Requst> allRequests;
//记录所有的请求
unordered_map<int, Add_Requst> saveAddReq;
unordered_map<int, Del_Requst> saveDelReq;
//输出
vector<string> res;
uint64_t serCost, powerCost;
//服务器选型
// string serName1,serName2;
//排序后的服务器数据
vector<pair<int, string>> sortMap;
//排序后的双节点
vector<pair<int, int>> sortTwo;
//排序后的单节点
vector<pair<int, int>> sortNoe;
#ifdef TEST
#endif
#ifdef UP

#endif
#ifdef RUN
string serName;
#endif

void DayPeocess()
{
    int day = 0;
    int preSerNum = 0;
    int currentNum = 0;
    scanf("%d", &day);
    SortSer(day);
    for (int i = 0; i < day; i++)
    {
        //清空一天的请求
        addRequests.clear();
        delRequests.clear();
        allRequests.clear();
        // migRequests.clear();
        int dayReq = 0;
        vector<string> migRes;
        scanf("%d", &dayReq);
        GetDayReq(dayReq);
        int newSerNum;
        migRes = migrate(migRequests, newSerNum);
        newSerNum = onServers.size();
        preSerNum = onServers.size();
        for (Add_Requst &request : twoNodeReq)
        {
            while (true)
            {
                Sel_Ser s1;
                s1.cores = 9999;
                for (Server &server : onServers)
                {
                    Sel_Ser s2 = Select(server, request);
                    if (s2.cores < s1.cores)
                    {
                        s1.cores = s2.cores;
                        s1.id = s2.id;
                    }
                }
                if (s1.cores != 9999)
                {
                    int ret = match(onServers[s1.id], request);
                    if (ret != false)
                    {
                        vmIdInfo[request._id] = {request._vmType, onServers[s1.id]._id, ret};
                        break;
                    }
                }
                else
                {
                    purchase(request);
                }
            }
        }
        for (Add_Requst &request : noeNodeReq)
        {
            while (true)
            {
                Sel_Ser s1;
                s1.cores = 9999;
                for (Server &server : onServers)
                {
                    Sel_Ser s2 = Select(server, request);
                    if (s2.cores < s1.cores)
                    {
                        s1.cores = s2.cores;
                        s1.id = s2.id;
                    }
                }
                if (s1.cores != 9999)
                {
                    int ret = match(onServers[s1.id], request);
                    if (ret != false)
                    {
                        vmIdInfo[request._id] = {request._vmType, onServers[s1.id]._id, ret};
                        break;
                    }
                }
                else
                {
                    purchase(request);
                }
            }
        }
        for (Del_Requst &requst : delRequests)
        {
            //    Vm tarVm = vmType[requst._id];
            auto &ret = vmIdInfo[requst._id];
            Server &ser = onServers[ret.serid];
            Vm_Type &vminfo = vmType[ret.vmName];
            vmIdInfo.erase(requst._id);
            switch (ret.ret)
            {
            case A:
                ser._A.cores += vminfo._cores;
                ser._A.mems += vminfo._mems;
                break;
            case B:
                ser._B.cores += vminfo._cores;
                ser._B.mems += vminfo._mems;
                break;
            case AB:
                ser._A.cores += vminfo._cores / 2;
                ser._B.cores += vminfo._cores / 2;
                ser._A.mems += vminfo._mems / 2;
                ser._B.mems += vminfo._mems / 2;
                break;
            }
        }
        currentNum = onServers.size();
        res.emplace_back("(purchase, " + to_string(serToBuy.size()) + ")");
        for (int i = preSerNum; i < currentNum; i++)
        {
            Server &ser = onServers[i];
            if (ser._mapid == -1)
            {
                int num = serToBuy[ser._serName];
                serCost += ser._hardCost * (uint64_t)num;
                res.emplace_back("(" + ser._serName + ", " + std::to_string(num) + ")");
                for (int j = i; j < currentNum; j++)
                {
                    if (ser._serName == onServers[j]._serName)
                    {
                        onServers[j]._mapid = preSerNum++;
                    }
                }
            }
        }
        serToBuy.clear();
        for (auto &r : migRes)
        {
            res.emplace_back(r);
        }
        for (Add_Requst &request : addRequests)
        {
            auto &ret = vmIdInfo[request._id];
            Server &ser = onServers[ret.serid];
            string str = "(" + std::to_string(ser._mapid);
            if (ret.ret == AB)
            {
                res.emplace_back(str + ")");
            }
            else if (ret.ret == B)
            {
                res.emplace_back(str + ", A)");
            }
            else
            {
                res.emplace_back(str + ", B)");
            }
        }
        for (Server &ser : onServers)
        {
            if (onServers.size() != 0)
                powerCost += ser._runCost;
        }
    }
}
int main()
{
#ifdef TEST
    std::freopen(filePath.c_str(), "rb", stdin);
#endif
#ifdef RUN
    cin >> serName;
#endif
    GetSerInfo();
    GetVmInfo();
    DayPeocess();
#ifdef UP
    for (auto &s : res)
        cout << s << endl;
#endif
#ifdef TEST
    for (auto &s : res)
        cout << s << endl;
    cout << serCost + powerCost << endl;
#endif
#ifdef RUN
    cout << serCost + powerCost << endl;
#endif
    return 0;
}

// for (auto iter = serType.begin(); iter != serType.end(); iter++)
// {
//     cout<<iter->second._serName<<endl;
// }

define.h

#ifndef _DEFINE_H
#define _DEFINE_H
#include <iostream>
#include <unordered_map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include "define.h"
#include "requests.h"
#include "server.h"
#include "vm.h"
#include "io.h"
#include "handle.h"
using namespace std;

#define can  35

enum{ADD, DEL, False, A, B, AB};
#define NOE 0
#define TWO 1
extern unordered_map<string, Server_Type> serType;
// extern unordered_map<string,Server_Type> serType;
extern unordered_map<string,Vm_Type> vmType;
//一天的增加请求存储
extern vector<Add_Requst> addRequests;
//一天的删除请求存储
extern vector<Del_Requst> delRequests;
extern vector<Server> onServers;
extern unordered_map<string,int> serToBuy;
extern vector<string> res;
extern unordered_map<int, Id_Map> vmIdInfo;
extern vector<All_Requst> allRequests;
// extern string serName1,serName2;
extern vector<pair<int,string>> sortMap;
//排序后的双节点
extern vector<pair<int, int>> sortTwo;
//排序后的单节点
extern vector<pair<int, int>> sortNoe;
//迁移请求
extern vector<Add_Requst> migRequests;
//一天中双节点增加的请求
extern vector<Add_Requst> twoNodeReq;
//一天中单节点增加的请求
extern vector<Add_Requst> noeNodeReq;

#endif

handle.cpp

#include "handle.h"

// void CheckTowNode(Add_Requst &addReq)
// {
//     //注意全新的一个比较
//     Vm_Type &vm = vmType[addReq._vmType];
//     if((serType[serName]._cores/2) < vm._cores/2 || (serType[serName]._mems) < vm._mems/2 )
//     {
//         cout<<"0"<<endl;
//         exit(1);
//     }
// }
// void CheckNoeNode(Add_Requst &addReq)
// {
//     Vm_Type &vm = vmType[addReq._vmType];
//     if((serType[serName]._cores/2 < vm._cores) || (serType[serName]._mems/2) < vm._mems)
//     {
//         cout<<"0"<<endl;
//         exit(1);
//     }
// }
int match(Server &ser, Add_Requst &addReq)
{
    Vm_Type &vm = vmType[addReq._vmType];
    bool OK = false;
    int cores = vm._cores, mems = vm._mems;
    if (vm._Node == TWO)
    {
        // CheckTowNode(addReq);
        cores /= 2;
        mems /= 2;
        OK = ser.fit(ser._A, cores, mems) && ser.fit(ser._B, cores, mems);
        if (OK)
        {
            ser._A.cores -= cores;
            ser._B.cores -= cores;
            ser._A.mems -= mems;
            ser._B.mems -= mems;
            return AB;
        }
    }
    else
    {
        // CheckNoeNode(addReq);
        OK = ser.fit(ser._A, cores, mems);
        if (OK)
        {
            ser._A.cores -= cores;
            ser._A.mems -= mems;
            return A;
        }
        else
        {
            OK = ser.fit(ser._B, cores, mems);
            if (OK)
            {
                ser._B.cores -= cores;
                ser._B.mems -= mems;
                return B;
            }
        }
    }
    return False;
}

vector<string> migrate(vector<Add_Requst> &migReq, int preNum)
{
    int migNum = 0;
    vector<string> res1;
    res1.emplace_back("(migration, " + to_string(migNum) + ")");
    return (res1);
}
Sel_Ser Select(Server &ser, Add_Requst &addReq)
{
    Vm_Type &vm = vmType[addReq._vmType];
    bool OK = false;
    int cores = vm._cores, mems = vm._mems;
    Sel_Ser ret;
    ret.cores = 9999;
    if (vm._Node == TWO)
    {
        cores /= 2;
        mems /= 2;
        OK = ser.fit(ser._A, cores, mems) && ser.fit(ser._B, cores, mems);
        if (OK)
        {
            ret.id = ser._id;
            ret.cores = min((ser._A.cores - cores)*0.6+(ser._A.mems - mems)*0.4, (ser._B.cores - cores)*0.6+(ser._B.mems - mems)*0.4);
            return ret;
        }
    }
    else
    {
        // CheckNoeNode(addReq);
        OK = ser.fit(ser._A, cores, mems);
        if (OK)
        {
            ret.id = ser._id;
            ret.cores = (ser._A.cores-cores)*0.6+(ser._A.mems-mems)*0.4;
            return ret;
        }
        else
        {
            OK = ser.fit(ser._B, cores, mems);
            if (OK)
            {
                ret.id = ser._id;
                ret.cores = (ser._B.cores - cores)*0.6 + (ser._B.mems - mems)*0.4;
                return ret;
            }
        }
    }
    return ret;
}
void purchase(Add_Requst &addReq)
{
    Server_Type ser;
    ser = serType[SelSer(addReq)];
    serToBuy[ser._serName]++;
    onServers.emplace_back(ser, onServers

以上是关于2021华为软件精英挑战赛的主要内容,如果未能解决你的问题,请参考以下文章

华为2017软件精英挑战赛初赛源码

华为软件精英挑战赛2016题解

夺冠秘诀?华为软件精英挑战赛两届冠军这样复盘比赛经验

2023第九届华为软件精英挑战赛启动报名,最高20万元奖金激励

2023第九届华为软件精英挑战赛启动报名,最高20万元奖金激励

2018 华为软件精英挑战赛 (上合赛区决赛:227/300分)