信息学奥赛一本通 第3章 第3节 堆及其应用(C++版)讲解.pptVIP

  • 67
  • 0
  • 约1.38万字
  • 约 56页
  • 2017-03-14 发布于湖北
  • 举报

信息学奥赛一本通 第3章 第3节 堆及其应用(C++版)讲解.ppt

get和put操作的复杂度均为log2n。所以建堆复杂度为nlog2n。合并果子时,每次需要从堆中取出两个数,然后再加入一个数,因此一次合并的复杂度为3log2n,共n-1次。所以整道题目的复杂度是nlog2n。 【参考程序】 #include iostream #include cstdio using namespace std; int heap_size, n; int heap[30001]; void swap(int a, int b) //加后变量可修改 { int t = a;a = b;b = t; } void put(int d) { int now, next; heap[++heap_size] = d; now = heap_size; while(now 1) { next = now 1; if(heap[now] = heap[next])return; swap(heap[now], heap[next]); now = next; } } int get() { int now, next, res; res = heap[1]; heap[1] = heap[heap_size--]; now = 1; while(now * 2 = heap_size) { next = n

文档评论(0)

1亿VIP精品文档

相关文档