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 KB

Problem 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.

解题思路:首先对二叉树进行中序遍历,得到数列,接下来的想法就是把这个中序遍历得到的数列进行权值修改,使其成为一个严格递增的数列。
关键的地方就是如何权值修改:假设中序遍历得到的数列为num[],假设第i个数字为num[i],第j个数字为num[j](i<j),为了保证严格递增,必须要num[j] - num[i] >= j - i,即第j个数至少比第i个数大j-i,变一下形就是:num[j] - j >= num[i] - i,即构成一个新的数列{num[i] - i},至此,我们的问题就变得很简单了,要修改最少的数,就可以先找其LIS,即这些是不需要修改的,修改的就是n-LIS。
#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;}


0 0
原创粉丝点击