XTU Binary Search Tree(LIS变形)
来源:互联网 发布:安卓4.0淘宝 编辑:程序博客网 时间:2024/06/08 18:18
Binary Search Tree
Accepted : 19 Submit : 45Time Limit : 1000 MS Memory Limit : 65536 KBProblem Description
In computer science, a binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
The left and right subtree must each also be a binary search tree.
There must be no duplicate nodes.
Now you are given a binary tree, and the key on each node.
Please help to answer the question, what is the least number of nodes do we have to modify to adjust the binary tree to a BST?
Notice, the key of a node can be modified to arbitrary real number.
Input
Multiple test cases. First line, an integer T ( 1 ≤ T ≤ 20 ), indicating the number of test cases.
For each test case, there will first be an empty line.
Then an integer n ( 1 ≤ n ≤ 5,000 ), indicating the number of nodes. You can assume nodes are numbered from 1 to n. After that, n lines follows.
On each of the n lines, say line i, indicating the information of node i. There are three integers key_i, L_i, R_i ( |key_i| ≤ 1,000,000,000, 1 ≤ L_i, R_i ≤ n ), indicating the key, left child, right child of the node. If one node does not have the left/right child, L_i/R_i will be zero.
Ouput
For each test case, output a line. The answer is an integer m ( 0 <= m <= n - 1 ), indicating the least number of nodes we have to modify.
Sample Input
232 2 33 0 01 0 0210 0 215 0 0
Sample Output
20
Hint
For test case 1, we can modify the key of node #2 to 1, and modify the key of node #3 to 3.
For test case 2, we do not need any modification.
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 5005;struct Node{int key;int l,r;}tree[maxn];int n,cnt,ind[maxn],num[maxn],d[maxn];void traverse(int rt){if(rt == 0) return;traverse(tree[rt].l);num[++cnt] = tree[rt].key;traverse(tree[rt].r);}int bisearch(int l,int r,int key){int mid;while(l <= r){mid = (l + r) >> 1;if(d[mid] == key) return mid;else if(d[mid] > key)r = mid - 1;else l = mid + 1;}return l;}int main(){int t,rt;scanf("%d",&t);while(t--){memset(ind,0,sizeof(ind));memset(tree,0,sizeof(tree));cnt = 0;scanf("%d",&n);for(int i = 1; i <= n; i++){scanf("%d%d%d",&tree[i].key,&tree[i].l,&tree[i].r);ind[tree[i].l]++;ind[tree[i].r]++;}for(int i = 1; i <= n; i++)if(ind[i] == 0){rt = i;break;}traverse(rt);for(int i = 1; i <= n; i++)num[i] -= i;int t,len = 1;d[1] = num[1];for(int i = 2; i <= n; i++){t = bisearch(1,len,num[i]);d[t] = num[i];len = max(len,t);}printf("%d\n",n - len);}return 0;}
- XTU Binary Search Tree(LIS变形)
- 二叉树-flatten binary search tree to linked lis
- search - binary search/sort tree
- Implement Binary Search Tree
- Binary Search Tree
- binary search tree
- Binary Search Tree
- Binary Search Tree
- Binary Search Tree 实现
- Validate Binary Search Tree
- binary search tree
- Unique Binary Search Tree
- Validate Binary Search Tree
- Recover Binary Search Tree
- Recover Binary Search Tree
- Validate Binary Search Tree
- Validate Binary Search Tree
- Recover Binary Search Tree
- LeetCode No332. Reconstruct Itinerary
- Java多线程初探——正确停止线程
- hdu5652 India and China Origins(并查集联通)
- 解决InvalidDataAccessApiUsageException: Executing an update/delete query
- 2016.06.04 教学妹弄gephi遇到的一些问题
- XTU Binary Search Tree(LIS变形)
- linux网络协议栈内核分析
- 中间件的简要概述
- Tamura纹理特征的matlab实现
- c++关于函数的按值传递和按址传递
- 机器学习-KMeans聚类 K值以及初始类簇中心点的选取
- 第2.7节 类型转换——表达式中float类型的操作数不会自动转化为double类型等
- MarkDown环境的配置-mac
- 【Java】majorityElement