C++信息竞赛进级第4讲 搜索剪枝.pptxVIP

  • 4
  • 0
  • 约5.02千字
  • 约 14页
  • 2024-05-30 发布于广西
  • 举报

学科竞赛编程教研研究院C++NOIPNOIIOI

题目描述在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n?1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。所以多多总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。合并果子

输入输出格式

输入输出样例

1PARTONEtemplatetypenameitemitemsmallest_heapitem::top(){returnheap[1];}templatetypenameitemintsmallest_heapitem::size(){returnlen;}templatetypenameitemboolsmallest_heapitem::empty(){returnlen;}smallest_heapinth;intn,ans;intmain(){cinn;for(inti=1;i=n;i++){inta;cina;h.push(a);}while(h.size()1){intx=h.top();h.pop();inty=h.top();h.pop();h.push(x+y);ans+=x+y;}coutans;return0;}#includeiostream#includecstringusingnamespacestd;templatetypenameitemclasssmallest_heap{private:itemheap[10001];intlen;public:smallest_heap();voidpush(itemconst);voidpop();itemtop();intsize();boolempty();};templatetypenameitemsmallest_heapitem::smallest_heap(){len=0;memset(heap,0,sizeof(heap));}templatetypenameitemvoidsmallest_heapitem::push(itemconstn){heap[++len]=n;intson=len,father=son/2;while(heap[son]heap[father]father=1){swap(heap[son],heap[father]);son=father,father=son/2;}}templatetypenameitemvoidsmallest_heapitem::pop(){swap(heap[1],heap[len]);heap[len--]=0;intfather=1,son=2;while(son=len){if(sonlenheap[son]heap[son+1])son++;if(heap[father]heap[son]){swap(heap[father],heap[son]);father=son,son=father*2;}elsebreak;}}

题目描述所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母。来看一个简单的例子:43#9865#045+8468#663344445509678其中$#$号代表被虫子啃掉的数字。根

文档评论(0)

1亿VIP精品文档

相关文档