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;}