POJ 3214 Heap 動態規劃法題解,poj3214

來源:互聯網
上載者:User

POJ 3214 Heap 動態規劃法題解,poj3214

Description

A (binary) heap is an array that can be viewed as a nearly complete binary tree. In this problem, we are talking about max-heaps.

A max-heap holds the property that for each node than the root, it’s key is no greater than its parent’s. Upon this we further require that for every node that has two children, key of any node in the subtree rooted at its left child should be less than that of any node in the subtree rooted at its right child.

Any array can be transformed into a max-heap satisfying the above requirement by modifying some of its keys. Your task is find the minimum number of keys that have to be modified.

Input

The input contains a single test case. The test case consists of nonnegative integers distributed on multiple lines. The first integer is the height of the heap. It will be at least 1 and at most 20. Then follow the elements of the array to be transformed into a heap described above, which do not exceed 109. Modified elements should remain integral though not necessarily nonnegative.

Output

Output only the minimum number of elements (or keys) that have to be modified.

Sample Input

313 61 4 3 8

Sample Output

4

Hint

   1                 10  / \               /  \ 3   6    =====>   3    9/ \ / \           / \  / \1 4 3 8           1 2  4 8

Source

POJ Monthly--2007.04.01, czh

本題雖然知道是動態規劃法,但是建模真的好難。

這裡是要把二叉樹轉換成為數組,然後求最長非遞減子序列的。

還需要注意處理其中的大於和大於等於的區別,這樣建模的難度真的很高呢。

#include <stdio.h>#include <vector>#include <string.h>#include <algorithm>#include <iostream>#include <string>#include <limits.h>#include <stack>#include <queue>#include <set>#include <map>using namespace std;const int MAX_N = 1 << 20 | 1;int tree[MAX_N], AN;//AN是arr的元素個數, tree下標從1開始int arr[MAX_N], id;//arr下標從0開始inline int lSon(int rt) { return rt << 1; }inline int rSon(int rt) { return rt << 1 | 1; }void postOrderToArr(int rt, int &span)//因為l<r,故此需要確保條件成立,就可以改成l <= r-1,使用span來確保所有的子樹都l<=r-1{if (lSon(rt) <= AN) postOrderToArr(lSon(rt), span);if (rSon(rt) <= AN) postOrderToArr(rSon(rt), ++span);arr[id++] = tree[rt] - span;}int biGetIndex(int low, int up, int val){while (low <= up){int m = low + ((up-low)>>1);if (arr[m] > val) up = m-1;else low = m+1;}return low;}int getLIS(){int cur = 0;for (int i = 1; i < AN; i++){if (arr[i] >= arr[cur]) arr[++cur] = arr[i];//這裡只是小小的最佳化,其實可以不用if else判斷else{arr[biGetIndex(0, cur, arr[i])] = arr[i];}}return cur+1;}int main(){int N;AN = 0;//arr 下標從1開始scanf("%d", &N);int a;while (~scanf("%d", &a)){tree[++AN] = a;}/*這樣計算有誤???for (int i = 0; i < N; i++){int j = 1 << i;for (int k = 0; k < j; k++){scanf("%d", tree + (++AN));}}*/id = 0;int span = 0;postOrderToArr(1, span);printf("%d\n", AN-getLIS());return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.