当前位置: 首页 > news >正文

华为OD 处理器

1. 题意

某公司研发了一款高性能 AI 处理器。每台物理设备具备 8 颗 AI 处理器,编号分别为 0、1、2、3、4、5、6、7。
编号 0-3 的处理器处于同一个链路中,编号 4-7 的处理器处于另外一个链路中,不通链路中的处理器不能通信。

现给定服务器可用的处理器编号数组array,以及任务申请的处理器数量num,找出符合下列亲和性调度原则的芯片组合。
如果不存在符合要求的组合,则返回空列表。
亲和性调度原则
申请个数为 1:选择同一链路,剩余可用处理器数量的优先级为 1 个 → 3 个 → 2 个 → 4 个。
申请个数为 2:选择同一链路,剩余可用处理器数量的优先级为 2 个 → 4 个 → 3 个。
申请个数为 4:必须选择同一链路,且该链路剩余可用处理器数量 恰好为 4 个。
申请个数为 8:申请节点所有 8 个处理器(即 0-7 号全选,需全部可用)。
提示
任务申请的处理器数量只能是 1、2、4、8。
链路划分:编号 0-3 为链路 1,编号 4-7 为链路 2。
处理器编号唯一,不存在重复编号。

输入输出
输入
输入包含 可用的处理器编号数组 array,以及 任务申请的处理器数量 num 两个部分:

第一行为 array(格式如 [0,1,4,5,6,7]);
第二行为 num(格式如 1)。

约束条件:

0 <= array.length <= 8(数组长度为 0 到 8,代表可用处理器数量);
0 <= array[i] <= 7(处理器编号只能是 0-7);
num ∈ [1, 2, 4, 8](申请数量只能是 1、2、4、8)。
输出
输出为 符合调度原则的处理器组合列表(按优先级排序)。
例如:
当 array = [0,1,4,5,6,7],num = 1 时,输出为:[[0],[1]]

2. 题解

我们需要将输入的处理器分为两组,大于3的一组和小于等于3的一组。

其次我们需要根据这两组中处理器的数目,来获取优先级,如果两者

优先级不一致,只对优先级高的进行处理。如果优先级一致的话,那么就

都需要进行处理。因此,我们可以事先维护一个固定的数组,用于存储

处理器的数目,优先级高的数目放在数组的后面,优先级低的放在数组的

前面。用一个函数来获取优先级。

在作处理时,我们需要在给定的数组中取给定的数目个,因此这就是一个

NNNKKK的回溯型问题。由于不能重复,因此我们需要一个bgbgbg

最后还是看代码吧!

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>using namespace std;int getLevel(int avail_n, int need_n)
{   const vector<int> _1lvl = {4,2,3,1};const vector<int> _2lvl = {3,4,2};const vector<int> _4lvl = {4};int ret = -1;auto getLvl = [&ret, avail_n](const vector<int> &lvl) {auto it = std::find(lvl.begin(), lvl.end(), avail_n);if ( it != lvl .end())ret = it - lvl.begin();};if ( need_n == 1) {getLvl( _1lvl);    }else if( need_n == 2) {getLvl( _2lvl);}else if ( need_n == 4) {getLvl(_4lvl);}return ret;
}void genSeqNK(const vector<int> &a,int K,int bg, vector<vector<int>> &ans, vector<int> &cur)
{if ( cur.size() == K) {ans.push_back( cur );return ;}for (int i = bg; i < a.size(); ++i) {cur.push_back( a[i] );genSeqNK( a, K, i + 1, ans, cur);cur.pop_back();}}int main()
{string s;getline( cin, s);vector<int> core1;vector<int> core2;int p = 0;int sz = s.size();for (int i = 1; i < sz; i += 2) {int v = s[i] - '0';if (v  > 3)core2.push_back(v);else core1.push_back(v);}int aval;cin >> aval;auto dump_arr = [](const vector<int> &arr) {cout << "[";for (auto it = arr.begin(); it != arr.end(); ++it) {if ( it != arr.begin())cout << ",";cout << *it;}cout << "]";};// dump_arr( core1 );// cout << "\n";// dump_arr( core2 );// cout << "\n";vector<vector<int>> ans_seq;int c1 = core1.size();int c2 = core2.size();//cout << c1 << " " << c2 << endl;    if (aval == 8 && c1 == c2 && c1 == 4) {ans_seq.push_back({0,1,2,3,4,5,6,7});}else {int lv1 = getLevel(c1, aval);int lv2 = getLevel(c2, aval);// cout << lv1 << " | "<<lv2 << endl;vector<int> tmp_seq;if ( lv1 != -1 && lv1 == lv2) {genSeqNK( core1, aval, 0, ans_seq, tmp_seq);genSeqNK( core2, aval, 0, ans_seq, tmp_seq);}else if ( lv1 > lv2 ) {genSeqNK( core1, aval, 0, ans_seq, tmp_seq);}else if ( lv2 > lv1 ){genSeqNK( core2, aval, 0, ans_seq, tmp_seq);}}cout << "[";for (auto it = ans_seq.begin(); it != ans_seq.end(); ++it) {if (it != ans_seq.begin()) cout << ",";cout << "[";for (auto it2 = it->begin(); it2 != it->end(); ++it2) {if ( it2 != it->begin())cout << ",";cout << *it2;}cout << "]";}cout << "]";return 0;
}

这里的getLevel用c++17的std::optional可能更加的合适!

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <optional>using namespace std;optional<int> getLevel(int avail_n, int need_n)
{   const vector<int> _1lvl = {4,2,3,1};const vector<int> _2lvl = {3,4,2};const vector<int> _4lvl = {4};optional<int> ret;auto getLvl = [&ret, avail_n](const vector<int> &lvl) {auto it = std::find(lvl.begin(), lvl.end(), avail_n);if ( it != lvl .end())ret = it - lvl.begin();};if ( need_n == 1) {getLvl( _1lvl);    }else if( need_n == 2) {getLvl( _2lvl);}else if ( need_n == 4) {getLvl(_4lvl);}return ret;
}void genSeqNK(const vector<int> &a,int K,int bg, vector<vector<int>> &ans, vector<int> &cur)
{if ( cur.size() == K) {ans.push_back( cur );return ;}for (int i = bg; i < a.size(); ++i) {cur.push_back( a[i] );genSeqNK( a, K, i + 1, ans, cur);cur.pop_back();}}int main()
{string s;getline( cin, s);vector<int> core1;vector<int> core2;int p = 0;int sz = s.size();for (int i = 1; i < sz; i += 2) {int v = s[i] - '0';if (v  > 3)core2.push_back(v);else core1.push_back(v);}int aval;cin >> aval;auto dump_arr = [](const vector<int> &arr) {cout << "[";for (auto it = arr.begin(); it != arr.end(); ++it) {if ( it != arr.begin())cout << ",";cout << *it;}cout << "]";};// dump_arr( core1 );// cout << "\n";// dump_arr( core2 );// cout << "\n";vector<vector<int>> ans_seq;int c1 = core1.size();int c2 = core2.size();//cout << c1 << " " << c2 << endl;    if (aval == 8 && c1 == c2 && c1 == 4) {ans_seq.push_back({0,1,2,3,4,5,6,7});}else {int lv1 = getLevel(c1, aval).value_or(-1);int lv2 = getLevel(c2, aval).value_or(-1);// cout << lv1 << " | "<<lv2 << endl;vector<int> tmp_seq;if ( lv1 != -1 && lv1 == lv2) {genSeqNK( core1, aval, 0, ans_seq, tmp_seq);genSeqNK( core2, aval, 0, ans_seq, tmp_seq);}else if ( lv1 > lv2 ) {genSeqNK( core1, aval, 0, ans_seq, tmp_seq);}else if ( lv2 > lv1 ){genSeqNK( core2, aval, 0, ans_seq, tmp_seq);}}cout << "[";for (auto it = ans_seq.begin(); it != ans_seq.end(); ++it) {if (it != ans_seq.begin()) cout << ",";cout << "[";for (auto it2 = it->begin(); it2 != it->end(); ++it2) {if ( it2 != it->begin())cout << ",";cout << *it2;}cout << "]";}cout << "]";return 0;
}

3. 参考

KJ.JK

http://www.xdnf.cn/news/1118215.html

相关文章:

  • 改进后的 OpenCV 5.x + GStreamer + Python 3.12 编译流程(适用于 Orange Pi / ARM64)
  • vue的优缺点
  • Vue 3 TypeScript 接口(Interface)使用
  • 【基于开源大模型(如deepseek)开发应用及其发展趋势的一点思考】
  • 西藏氆氇新生:牦牛绒混搭液态金属的先锋尝试
  • web:js的三种引用方式
  • MYSQL笔记1
  • 大模型之Langchain篇(二)——RAG
  • SQL的初步学习(二)(以MySQL为例)
  • 《区间dp》
  • Excalidraw:一款颠覆传统思维的免费开源绘图工具
  • DHS及HTTPS工作过程
  • JSON/AJAX/XHR/FetchAPI知识点学习整理
  • 代码随想录算法训练营第三十二天|动态规划理论基础、LeetCode 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
  • std::sort的核心设计思想
  • 代码随想录算法训练营第十七天
  • MongoDB数据基本介绍
  • 从 Intel MacBook 迁移到 ARM MacBook 的完整指南
  • Windows怎样同步时间服务器?
  • 【网络实验】-BGP选路原则-11条
  • 攻防世界——Web题 very_easy_sql
  • 嵌入式 Linux开发环境构建之安装 SSH 软件
  • Spring AI 项目实战(十六):Spring Boot + AI + 通义万相图像生成工具全栈项目实战(附完整源码)
  • mapstruct与lombok冲突原因及解决方案
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 44(题目+回答)
  • vue2入门(1)vue核心语法详解复习笔记
  • Agent篇
  • [Linux入门 ] RAID存储技术概述
  • 面向对象设计模式详解
  • 基于 STM32H743VIT6 的边缘 AI 实践:猫咪叫声分类 CNN 网络部署实战(已验证)中一些bug总结