天长市做网站买了个网站后怎么做
2026/4/18 19:30:23 网站建设 项目流程
天长市做网站,买了个网站后怎么做,网站风格发展趋势,游戏开发app魔法收积木 2025华为OD机试双机位C卷 - 华为OD上机考试双机位C卷 200分题型 华为OD机试双机位C卷真题目录点击查看: 华为OD机试双机位C卷真题题库目录#xff5c;机考题库 算法考点详解 题目描述 考友反馈题目大意:现在有n堆积木#xff0c;每堆积木都有正整数数量#…魔法收积木2025华为OD机试双机位C卷 - 华为OD上机考试双机位C卷 200分题型华为OD机试双机位C卷真题目录点击查看: 华为OD机试双机位C卷真题题库目录机考题库 算法考点详解题目描述考友反馈题目大意:现在有n堆积木每堆积木都有正整数数量魔法一次可以把积木数量一致的积木堆砍半要求出使用魔法收完积木堆的最少要用多少次魔法。输入描述第一行输入n代表积木的堆数第二行输入n个正整数用空格分割表示每堆积木的个数。输出描述输出使用魔法收完积木堆的最少要用多少次魔法。用例1输入4 4 4 4 4输出3用例2输入2 3 4输出4题解思路贪心魔法一次可以把积木数量一致的积木堆砍半为了让使用魔法次数少就是尽可能让一次魔法处理尽量多堆的积木。基于1可以推导得到先将最大的值砍半尽可能让它和小的值一同进行处理。这道题的基本逻辑就是1. 统计所有积木堆使用哈希表存储不同大小积木堆的个数2. 每次将积木最多堆数量(x)砍半使用魔法数量1更新哈希表mp[x/2] mp[x]3. 不断重复2的逻辑直到所有堆积木数量都变为0结束。根据3的逻辑主要是跟踪最大积木数量对于java和C都有对用的map使用python、c和go可以使用优先队列 map去处理。c#includeiostream #includevector #includestring #include utility #include sstream #includealgorithm #includecmath #includemap using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin n; // 会自动按照key排序 记录不同数量积木数量 maplong, long mp; for (int i 0; i n; i) { long cnt; cin cnt; mp[cnt]; } // 结果 long ans 0; while (!mp.empty()) { auto it prev(mp.end()); long x it-first; long cnt it-second; mp.erase(it); ans; long nextX x 1; if (nextX 0) { mp[nextX] cnt; } } cout ans; return 0; }JAVAimport java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int n Integer.parseInt(br.readLine().trim()); // TreeMap有序 map等价于 C map // key积木数量value该数量出现的次数 TreeMapLong, Long mp new TreeMap(); st new StringTokenizer(br.readLine()); for (int i 0; i n; i) { long x Long.parseLong(st.nextToken()); mp.put(x, mp.getOrDefault(x, 0L) 1); } long ans 0; while (!mp.isEmpty()) { // 取当前最大 key等价于 prev(mp.end()) Map.EntryLong, Long entry mp.lastEntry(); long x entry.getKey(); long cnt entry.getValue(); mp.pollLastEntry(); // 删除最大 key ans; long nextX x 1; if (nextX 0) { mp.put(nextX, mp.getOrDefault(nextX, 0L) cnt); } } System.out.println(ans); } }PythonimportsysimportheapqfromcollectionsimportCounterdefmain():# 读取全部输入datasys.stdin.read().strip().split()nint(data[0])numslist(map(int,data[1:1n]))# 统计每种积木数量出现次数cntCounter(nums)# 用最大堆模拟取最大 key python默认是最小堆所以使用负数heap[-xforxincnt.keys()]heapq.heapify(heap)ans0whileheap:x-heapq.heappop(heap)ccnt.pop(x)ans1nxx1ifnx0:ifnxnotincnt:heapq.heappush(heap,-nx)cnt[nx]cprint(ans)if__name____main__:main()JavaScriptuse strict;constreadlinerequire(readline);constrlreadline.createInterface({input:process.stdin,output:process.stdout});letlines[];rl.on(line,line{if(line.trim())lines.push(line.trim());});// 手写最大堆classMaxHeap{constructor(){this.data[];}size(){returnthis.data.length;}// 插入元素push(val){this.data.push(val);this._siftUp(this.data.length-1);}// 弹出最大元素pop(){if(this.data.length0)returnnull;consttopthis.data[0];constlastthis.data.pop();if(this.data.length0){this.data[0]last;this._siftDown(0);}returntop;}_siftUp(i){while(i0){constpMath.floor((i-1)/2);if(this.data[i]this.data[p])break;[this.data[i],this.data[p]][this.data[p],this.data[i]];ip;}}_siftDown(i){constnthis.data.length;while(true){letmaxIdxi;constl2*i1,r2*i2;if(lnthis.data[l]this.data[maxIdx])maxIdxl;if(rnthis.data[r]this.data[maxIdx])maxIdxr;if(maxIdxi)break;[this.data[i],this.data[maxIdx]][this.data[maxIdx],this.data[i]];imaxIdx;}}}rl.on(close,(){letidx0;constnNumber(lines[idx]);constnumslines[idx].split(/\s/).map(Number);// Map: 记录每种积木数量出现的次数constmpnewMap();for(leti0;in;i){constxnums[i];mp.set(x,(mp.get(x)||0)1);}constheapnewMaxHeap();for(constkeyofmp.keys())heap.push(key);letans0;while(heap.size()0){// 最大数量个数letxheap.pop();constcntmp.get(x);mp.delete(x);ans;constnextXx1;if(nextX0){if(!mp.has(nextX))heap.push(nextX);mp.set(nextX,(mp.get(nextX)||0)cnt);}}console.log(ans);});Gopackagemainimport(bufiocontainer/heapfmtos)// 最大堆int64typeMaxHeap[]int64func(h MaxHeap)Len()int{returnlen(h)}func(h MaxHeap)Less(i,jint)bool{returnh[i]h[j]}// 大顶堆func(h MaxHeap)Swap(i,jint){h[i],h[j]h[j],h[i]}func(h*MaxHeap)Push(xinterface{}){*happend(*h,x.(int64))}func(h*MaxHeap)Pop()interface{}{old:*h n:len(old)x:old[n-1]*hold[:n-1]returnx}funcmain(){in:bufio.NewReader(os.Stdin)varnintfmt.Fscan(in,n)// 记录不同数量积木数量mp:make(map[int64]int64)// 最大堆h:MaxHeap{}heap.Init(h)fori:0;in;i{varxint64fmt.Fscan(in,x)ifmp[x]0{heap.Push(h,x)}mp[x]}// 结果varansint640forh.Len()0{// 取最大x:heap.Pop(h).(int64)ifmp[x]0{continue}cnt:mp[x]delete(mp,x)ansnx:x1ifnx0{ifmp[nx]0{heap.Push(h,nx)}mp[nx]cnt}}fmt.Println(ans)}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询