华为OD机试2025C卷-流水线[100分]( Java _ Python3 _ C++ _ C语言 _ JsNode _ Go)实现100%通过率
个人主页深夜coding算法 专栏系列2026年华为最新OD机试题库详解 一次订阅永久解锁 | 持续更新100篇 | 6语言全覆盖文章目录❄️前言☀️一题目描述 题目名称 题目内容 输入描述 输出描述 示例☀️二解题思路☀️三代码实现CJavaPython3C语言JavaScriptGo☀️四复杂度分析⭐ 五易错点坑1不是FCFS坑2M可能大于N共勉❄️前言流水线调度——N个任务分配到M条流水线每条线串行执行求最短完成时间。一看就是贪心每次把下一个任务分配给当前总时间最小的流水线。☀️一题目描述 题目名称流水线 题目内容有 M 条相同的流水线每条流水线一次只能处理一个任务。共有 N 个任务每个任务的耗时分别为time[i]。任务可以分配到任意一条流水线上一条流水线上的任务串行执行。求完成所有任务的最短时间。 输入描述第一行M 和 N第二行N 个正整数表示每个任务的耗时 输出描述输出一个整数最短完成时间 示例输入 3 5 8 4 3 2 10 输出 12 说明三条流水线分配 [8],[4,2,10],[3] → 完成时间 12☀️二解题思路任务按耗时降序排序。维护优先队列M条流水线的当前累计时间。每次弹出最小值加上当前任务耗时再放回。最终答案 队列最大值。☀️三代码实现C#includeiostream#includequeue#includealgorithm#includefunctionalusingnamespacestd;intmain(){intM,N;cinMN;vectorintt(N);for(inti0;iN;i)cint[i];sort(t.begin(),t.end(),greaterint());priority_queueint,vectorint,greaterintpq;for(inti0;iM;i)pq.push(0);for(intx:t){intcurpq.top();pq.pop();pq.push(curx);}intans0;while(!pq.empty()){ansmax(ans,pq.top());pq.pop();}coutansendl;}Javaimportjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){ScannerscnewScanner(System.in);intMsc.nextInt(),Nsc.nextInt();Integer[]tnewInteger[N];for(inti0;iN;i)t[i]sc.nextInt();Arrays.sort(t,Collections.reverseOrder());PriorityQueueIntegerpqnewPriorityQueue();for(inti0;iM;i)pq.offer(0);for(intx:t)pq.offer(pq.poll()x);intans0;while(!pq.isEmpty())ansMath.max(ans,pq.poll());System.out.println(ans);}}Python3importheapq M,Nmap(int,input().split())timeslist(map(int,input().split()))times.sort(reverseTrue)pq[0]*Mforxintimes:curheapq.heappop(pq)heapq.heappush(pq,curx)print(max(pq))C语言#includestdio.h#includestdlib.hintcmp(constvoid*a,constvoid*b){return*(int*)b-*(int*)a;}intmain(){intM,N,t[1024],pq[128]{0};scanf(%d %d,M,N);for(inti0;iN;i)scanf(%d,t[i]);qsort(t,N,sizeof(int),cmp);for(inti0;iN;i){intmi0;for(intj1;jM;j)if(pq[j]pq[mi])mij;pq[mi]t[i];}intans0;for(inti0;iM;i)if(pq[i]ans)anspq[i];printf(%d\n,ans);}JavaScriptconst[mn,arr]require(fs).readFileSync(0,utf-8).trim().split(\n);const[M,N]mn.split( ).map(Number);consttimesarr.split( ).map(Number).sort((a,b)b-a);constpqArray(M).fill(0);for(constxoftimes){letmipq.indexOf(Math.min(...pq));pq[mi]x;}console.log(Math.max(...pq));Gopackagemainimport(fmt;sort;container/heap)typeIntHeap[]intfunc(h IntHeap)Len()int{returnlen(h)}func(h IntHeap)Less(i,jint)bool{returnh[i]h[j]}func(h IntHeap)Swap(i,jint){h[i],h[j]h[j],h[i]}func(h*IntHeap)Push(x any){*happend(*h,x.(int))}func(h*IntHeap)Pop()any{n:len(*h)-1;x:(*h)[n];*h(*h)[:n];returnx}funcmain(){varM,Nintfmt.Scan(M,N)t:make([]int,N)fori:ranget{fmt.Scan(t[i])}sort.Sort(sort.Reverse(sort.IntSlice(t)))h:IntHeap{}fori:0;iM;i{heap.Push(h,0)}for_,x:ranget{cur:heap.Pop(h).(int)heap.Push(h,curx)}ans:0forh.Len()0{v:heap.Pop(h).(int);ifvans{ansv}}fmt.Println(ans)}☀️四复杂度分析指标数值时间复杂度O(N log M)空间复杂度O(M)⭐ 五易错点坑1不是FCFS大任务先分配可以缩短总时间类比先装大石头。坑2M可能大于N流水线比任务多时答案是最大的单个任务耗时。共勉任务调度题的核心就是优先队列每次喂给最闲的那条线。关于本专栏一次订阅永久解锁全部100篇真题详解6语言全覆盖Java | Python3 | C | C语言 | JsNode | Go

相关新闻