Heap_堆_2018_2_20
2018-02-20 09:04
169 查看
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
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
3 1 3 6 1 4 3 8Sample Output
4Hint
1 10 / \ / \ 3 6 =====> 3 9 / \ / \ / \ / \ 1 4 3 8 1 2 4 8
#include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int> v; vector<int> vv; int cnt=0,delta; void dfs(int x){ if(2*x+1<cnt)dfs(2*x+1); if(2*x+2<cnt){ delta++; dfs(2*x+2); } v[x]-=delta; if(vv.empty()||v[x]>=vv.back()) vv.push_back(v[x]); else *upper_bound(vv.begin(),vv.end(),v[x])=v[x]; } int main(){ int n; scanf("%d",&n); int x; while(~scanf("%d",&x)) v.push_back(x); cnt=v.size(); dfs(0); printf("%d\n",cnt-vv.size()); }
相关文章推荐
- 2018_1_20_poj_A Contesting Decision
- 2018_1_20_next_permutation_排列
- 2018_3_20 异常
- 2018_1_20_The Circumference of the Circle_圆周_2242
- 1011_C/C++笔试题_20:heap和stack区别
- 2018_1_20_Ugly Numbers_打表
- 2018_3_20
- 2018_1_20_ujn_oj
- 2018_1_20_quicksum,poj3094,水题
- PAT L1-044. 稳赢2018_2_20
- 2018_03_20牛客网刷题_JAVA基础知识
- Java笔记1/20/2018
- 2018_1_20_Number Sequence_古怪
- Binary Search Heap Construction_tread_2018_2_18
- 人脸检测总结报告(2018_1_20-2018_1_30)
- 2018_2_7_10-20-30_二重哈希_开放寻址_扑克牌
- 1069. The Black Hole of Numbers (20)
- 【PAT】 A1005. Spell It Right (20)
- 2018.02.02【GDOI2018】模拟C组——最大配对
- PAT 1041. Be Unique (20)