当前位置: 代码迷 >> 综合 >> 2021华为软件精英挑战赛的baseLine,Java版,仅供参考,无核心算法
  详细解决方案

2021华为软件精英挑战赛的baseLine,Java版,仅供参考,无核心算法

热度:39   发布时间:2023-11-25 08:54:20.0

2021华为软件精英挑战赛

给出我的baseLine

package com.huawei.java.main;import java.util.*;public class Main {
    private static  long TOTAL_COST = 0;private static  long POWER_COST = 0;private static  long SERVER_COST = 0;//服务器信息public static HashMap<String, List<Integer>> serverInfos = new HashMap<>();//虚拟机信息public static HashMap<String, List<Integer>> vmInfos = new HashMap<>();//请求信息public static List<List<String>> requestInfos = new ArrayList<>();//购买服务器信息public static int serverNums = 0;public static HashMap<Integer,List<Integer>> buyServerResource = new HashMap<>();//部署信息public static ArrayList<Integer> serverRunvms = new ArrayList<>();//记录虚拟机运行在服务器上的信息public static HashMap<String,List<Integer>> vmOnSevrer = new HashMap<>();//记录输出public static List<String> result = new ArrayList<>();public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);//将服务器封装到集合serverinfos中int serverNumbers = Integer.parseInt(scanner.nextLine());for (int i = 0; i < serverNumbers; i++) {
    String serverInfo_init = scanner.nextLine();getserverdata(serverInfo_init);}//将虚拟机封装到集合vminfos中int vmNums = Integer.parseInt(scanner.nextLine());for (int i = 0; i < vmNums; i++) {
    String vmInfo_init = scanner.nextLine();getvmdata(vmInfo_init);}//购买服务器buyServer();//将每天请求序列封装到集合requestInfos中int requestdays = Integer.parseInt(scanner.nextLine());for (int day = 0; day < requestdays; day++) {
    int oneDayReqNums = Integer.parseInt(scanner.nextLine());requestInfos.clear();for (int j = 0; j < oneDayReqNums; j++) {
    String request_init = scanner.nextLine();getRequest(request_init);}//进行匹配match(day);//耗能计算serverPower();}// System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
// System.out.println();
// System.out.println("服务器费用:"+SERVER_COST);
// System.out.println("服务器耗能费用:"+POWER_COST);TOTAL_COST = SERVER_COST+POWER_COST;
// System.out.println("总费用:"+TOTAL_COST);for (String s : result) {
    System.out.println(s);}}/*** 服务器能耗计算*/private static void serverPower() {
    for (int i = 0; i < serverRunvms.size(); i++) {
    if (serverRunvms.get(i) != 0){
    POWER_COST += buyServerResource.get(i).get(5);}}}/*** 为虚拟机匹配服务器* @param day*/private static void match(int day) {
    if (day != 0){
    expansion();}migrate();for (List<String> requestInfo : requestInfos) {
    if (requestInfo.size() == 3){
    int resourceEnough = createVM(requestInfo);}else {
    delVM(requestInfo);}}}//初始化购买虚拟机public static void buyServer() {
    String serverType = "hostVIHCW";
// String serverType = "NV603";int n = 10000;for (int i = 0; i < n; i++) {
    serverRunvms.add(0);}String initBuy = "(purchase, ";initBuy += Integer.toString(1)+")";//添加购买服务器的信息result.add(initBuy);String buyinfo= "("+ serverType +", ";buyinfo += Integer.toString(n) + ")";//添加购买服务器型号和数量的信息result.add(buyinfo);for (int i = 0; i < n; i++) {
    buyServerResource.put(serverNums++, (ArrayList<Integer>) serverInfos.get(serverType));SERVER_COST += serverInfos.get(serverType).get(4);}// serverType = "hostUY41I";
 serverType = "NV604";
// //添加购买服务器的信息
// buyinfo= "("+ serverType +", ";
// buyinfo += Integer.toString(serverNums) + ")\n";
// //添加购买服务器型号和数量的信息
// result.add(buyinfo);
//
// for (int i = 0; i < n/2 ; i++) {
    
// buyServerResource.put(serverNums++,serverInfos.get(serverType));
// SERVER_COST += serverInfos.get(serverType).get(4);
// }}/***删除虚拟机* @param requestInfo*/private static void delVM(List<String> requestInfo) {
    String reqVmId = requestInfo.get(1);List<Integer> vmOnServerData = vmOnSevrer.get(reqVmId);int serverId = vmOnServerData.get(0);int vms = serverRunvms.get(serverId) - 1;serverRunvms.set(serverId,vms);List<Integer> resourcedata = buyServerResource.get(serverId);if (vmOnServerData.size() == 5){
    int needCore = vmOnServerData.get(1)/2,memory = vmOnServerData.get(2)/2;ArrayList<Integer> serverData = new ArrayList<>();int serverCoreA = buyServerResource.get(serverId).get(0) + needCore;serverData.add(serverCoreA);int serverCoreB = buyServerResource.get(serverId).get(1) + needCore;serverData.add(serverCoreB);int serverMemoryA = buyServerResource.get(serverId).get(2) + memory;serverData.add(serverMemoryA);int serverMemoryB = buyServerResource.get(serverId).get(3) + memory;serverData.add(serverMemoryB);serverData.add(resourcedata.get(4));serverData.add(resourcedata.get(5));buyServerResource.put(serverId,serverData);}else{
    int cores = vmOnServerData.get(1),memory = vmOnServerData.get(2);if (vmOnServerData.get(3) == 1){
    ArrayList<Integer> serverData = new ArrayList<>();int serverCoreA = buyServerResource.get(serverId).get(0) + cores;serverData.add(serverCoreA);serverData.add(resourcedata.get(1));int serverMemoryA = buyServerResource.get(serverId).get(2) + memory;serverData.add(serverMemoryA);serverData.add(resourcedata.get(3));serverData.add(resourcedata.get(4));serverData.add(resourcedata.get(5));buyServerResource.put(serverId,serverData);}else {
    ArrayList<Integer> serverData = new ArrayList<>();int serverCoreB = buyServerResource.get(serverId).get(1) + cores;serverData.add(resourcedata.get(0));serverData.add(serverCoreB);int serverMemoryB = buyServerResource.get(serverId).get(3) + memory;serverData.add(resourcedata.get(2));serverData.add(serverMemoryB);serverData.add(resourcedata.get(4));serverData.add(resourcedata.get(5));buyServerResource.put(serverId,serverData);}}}/*** 创建虚拟机* @param requestInfo* @return -1:失败,1:成功*/private static int createVM(List<String> requestInfo) {
    String VmType = requestInfo.get(1);String reqVmId = requestInfo.get(2);List<Integer> vmData = vmInfos.get(VmType);int success = -1;for (int i = 0; i < serverNums; i++) {
    List<Integer> source = buyServerResource.get(i);if(choseServer(source,vmData,i,reqVmId)){
    int vms = serverRunvms.get(i) + 1;serverRunvms.set(i,vms);success = 1;break;}}return success;}private static boolean choseServer(List<Integer> resourcedata, List<Integer> vmData, int serverId, String reqVmId) {
    int vmCores = vmData.get(0);int vmMemeory = vmData.get(1);int vmTwoNodes = vmData.get(2);List<Integer> vmOnSevrerData = new ArrayList<> ();int serverCoreA = resourcedata.get(0);int serverCoreB = resourcedata.get(1);int serverMemoryA = resourcedata.get(2);int serverMemoryB = resourcedata.get(3);if (vmTwoNodes == 1){
     //双节点int needCore = vmCores/2,needMemory = vmMemeory/2;if (serverCoreA >= needCore && serverCoreB >= needCore && serverMemoryA >= needMemory && serverMemoryB >= needMemory){
    ArrayList<Integer> serverData = new ArrayList<>();serverCoreA -= needCore;serverData.add(serverCoreA);serverCoreB -= needCore;serverData.add(serverCoreB);serverMemoryA -= needMemory;serverData.add(serverMemoryA);serverMemoryB -= needMemory;serverData.add(serverMemoryB);serverData.add(resourcedata.get(4));serverData.add(resourcedata.get(5));buyServerResource.put(serverId,serverData);//记录信息vmOnSevrerData.add(serverId);vmOnSevrerData.add(vmCores);vmOnSevrerData.add(vmMemeory);vmOnSevrerData.add(1);vmOnSevrerData.add(2);vmOnSevrer.put(reqVmId,vmOnSevrerData);//记录输出
// for (Integer resourcedatum : resourcedata) {
    
// System.out.println(resourcedatum);
// }result.add("("+Integer.toString(serverId)+")");return true;}else {
    return false;}}else if (serverCoreA >= vmCores && serverMemoryA >= vmMemeory){
    //单节点AArrayList<Integer> serverData = new ArrayList<>();serverCoreA -= vmCores;serverData.add(serverCoreA);serverData.add(resourcedata.get(1));serverMemoryA -= vmMemeory;serverData.add(serverMemoryA);serverData.add(resourcedata.get(3));serverData.add(resourcedata.get(4));serverData.add(resourcedata.get(5));buyServerResource.put(serverId,serverData);vmOnSevrerData.add(serverId);vmOnSevrerData.add(vmCores);vmOnSevrerData.add(vmMemeory);vmOnSevrerData.add(1);vmOnSevrer.put(reqVmId,vmOnSevrerData);//记录输出result.add("("+Integer.toString(serverId)+", A)");return true;}else if (serverCoreB >= vmCores && serverMemoryB >= vmMemeory){
    //单节点BArrayList<Integer> serverData = new ArrayList<>();serverCoreB -= vmCores;serverData.add(resourcedata.get(0));serverData.add(serverCoreA);serverMemoryB -= vmMemeory;serverData.add(resourcedata.get(2));serverData.add(serverMemoryA);serverData.add(resourcedata.get(4));serverData.add(resourcedata.get(5));buyServerResource.put(serverId,serverData);vmOnSevrerData.add(serverId);vmOnSevrerData.add(vmCores);vmOnSevrerData.add(vmMemeory);vmOnSevrerData.add(2);vmOnSevrer.put(reqVmId,vmOnSevrerData);//记录输出result.add("("+Integer.toString(serverId)+", B)");return true;}return false;}
//迁移虚拟机private static void migrate() {
    String s = "(migration, 0)";result.add(s);}
//扩容服务器private static void expansion() {
    String s = "(purchase, 0)";result.add(s);}/*** 解析服务器* @param serverInfo_init*/public static void getserverdata(String serverInfo_init) {
    List<Integer> list = new ArrayList<Integer>();String serverType = "";String[] serverInfo_init_split = serverInfo_init.split(", ");serverType = serverInfo_init_split[0].substring(1);list.add(Integer.parseInt(serverInfo_init_split[1])/2);list.add(Integer.parseInt(serverInfo_init_split[1])/2);list.add(Integer.parseInt(serverInfo_init_split[2])/2);list.add(Integer.parseInt(serverInfo_init_split[2])/2);list.add(Integer.parseInt(serverInfo_init_split[3]));list.add(Integer.parseInt(serverInfo_init_split[4].substring(0, serverInfo_init_split[4].length()-1)));serverInfos.put(serverType, list);}/*** 解析虚拟机* @param vmInfo_init*/public static void getvmdata(String vmInfo_init){
    List<Integer> list = new ArrayList<Integer>();String vmType = "";String[] vmInfo_init_split = vmInfo_init.split(", ");vmType = vmInfo_init_split[0].substring(1);for (int i = 1; i < 3; i++) {
    list.add(Integer.parseInt(vmInfo_init_split[i]));}list.add(Integer.parseInt(vmInfo_init_split[3].substring(0, vmInfo_init_split[3].length()-1)));vmInfos.put(vmType,list);}/*** 解析请求序列* @param request_init*/public static void getRequest(String request_init){
    List<String> innerlist = new ArrayList<>();String opName = "";String[] request_init_split = request_init.split(", ");opName = request_init_split[0].substring(1);if ("add".equals(opName)){
    innerlist.add(opName);innerlist.add(request_init_split[1]);innerlist.add(request_init_split[2].substring(0,request_init_split[2].length()-1));}else{
    innerlist.add(opName);innerlist.add(request_init_split[1].substring(0,request_init_split[1].length()-1));}requestInfos.add(innerlist);}
}

本地运行成功

可是提交后总是报部署虚拟机超出服务器资源限制。改编于知乎:ddmm

有兴趣的拿去看看吧!

  相关解决方案