nyoj 1278-Prototypes analyze //数据结构(指针瞎搞)
- 内存限制:64MB 时间限制:1000ms 特判: No
- 通过数:19 提交数:57 难度:2
题目描述:
ALpha Ceiling Manufacturers (ACM) is analyzing the properties of its new series of Incredibly Collapse-Proof Ceilings (ICPCs). An ICPC consists of n layers of material, each with a different value of collapse resistance (measured as a positive integer). The analysis ACM wants to run will take the collapse-resistance values of the layers, store them in a binary search tree, and check whether the shape of this tree in any way correlates with the quality of the whole construction. Because, well, why should it not? To be precise, ACM takes the collapse-resistance values for the layers, ordered from the top layer to the bottom layer, and inserts them one-by-one into a tree. The rules for inserting a value v are:
• If the tree is empty, make v the root of the tree.
• If the tree is not empty, compare v with the root of the tree.
• If v is smaller, insert v into the left subtree of the root,
• otherwise insert v into the right subtree.
ACM has a set of ceiling prototypes it wants to analyze by trying to collapse them. It wants to take each group of ceiling prototypes that have trees of the same shape and analyze them together. For example , assume ACM is considering five ceiling prototypes with three layers each, as described by Sample Input 1 and shown in Figure C.1. Notice that the first prototype’s top layer has collapseresistance value 2, the middle layer has value 7, and the bottom layer has value 1. The second prototype has layers with collapse-resistance values of 3, 1, and 4 – and yet these two prototypes induce the same tree shape, so ACM will analyze them together. Given a set of prototypes, your task is to determine how many different tree shapes they induce.
输入描述:
The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8). Each test case specifies : ● Line 1: two integers n (1 ≤ n ≤ 50), which is the number of ceiling prototypes to analyze, and k (1 ≤ k ≤ 20), which is the number of layers in each of the prototypes. ● The next n lines describe the ceiling prototypes. Each of these lines contains k distinct integers ( between 1 and 1e6, inclusive ) , which are the collapse-resistance values of the layers in a ceiling prototype, ordered from top to bottom.
输出描述:
For each test case generate a single line containing a single integer that is the number of different tree shapes.
样例输入:
1 5 3 2 7 1 1 5 9 3 1 4 2 6 5 9 7 3样例输出:
4
题意,给你一个序列,先将它建成平衡树,然后判断有多少个不一样的树型。
一开始我以为给出来的序列是先序遍历的序列。。。然后wawawa)
然后将错就错,建树之后输出先序遍历,然后再进行递归暴力匹配,数据小,瞎搞
#include<bits/stdc++.h>
using namespace std;
const int max_n=1e5+5;
int mp[100][100];
int k;
typedef struct node{//二叉树
struct node *l,*r;
int date;
}Node,*T;
T tree[55];
void init(T &t,int x){//初始化
t=new Node;
t->l=NULL;
t->r=NULL;
t->date=x;
}
void ins(T &t,int x){//插入
if(t==NULL){
t=new Node;
t->date=x;
t->l=NULL;t->r=NULL;
return;
}
if(x<t->date) ins(t->l,x);
else ins(t->r,x);
}
void dfs(T t,int i,int &cnt){//先序遍历
if(t==NULL) return ;
mp[i][++cnt]=t->date;
dfs(t->l,i,cnt);
dfs(t->r,i,cnt);
}
void f(int x,int l,int r,int &book,int &m){//找出分段点和判断左右子树
if(mp[x][l+1]<mp[x][l]) {
book=1;
for(int i=l+1;i<=r;i++){
if(mp[x][i]>=mp[x][l]){
m=i;break;
}
}
}
else {
book=2;
for(int i=l+1;i<=r;i++){
if(mp[x][i]<mp[x][l]){
m=i;break;
}
}
}
}
void cmp(int x,int y,int lx,int rx,int ly,int ry){//匹配对比
if(lx==rx) return;
int m1=-1,m2=-1,book1,book2;
f(x,lx,rx,book1,m1);
f(y,ly,ry,book2,m2);
if(m1==-1||m2==-1){//无左子树或者无右子树
if(m1==m2&&book1==book2){
cmp(x,y,lx+1,rx,ly+1,ry);
}
else k=1;
return;
}
if(book1==book2){//左右在一边
if(m1!=m2) {k=1;return ;}
cmp(x,y,lx+1,m1-1,ly+1,m2-1);
cmp(x,y,m1,rx,m2,ry);
}
if(book1!=book2){//左右岔开的
int a=m1-lx-1,b=ry-m2+1;
if(a!=b) {k=1;return ;}
cmp(x,y,lx+1,m1-1,m2,ry);
cmp(x,y,m1,rx,ly+1,m2-1);
}
}
int main(){
int T;scanf("%d",&T);
while(T--){
int n,kk;scanf("%d%d",&n,&kk);
for(int i=1;i<=n;i++){
for(int j=1;j<=kk;j++){
int x;scanf("%d",&x);
if(j==1) init(tree[i],x);
else ins(tree[i],x);
}
}
for(int i=1;i<=n;i++){
int cnt=0;
dfs(tree[i],i,cnt);
}
int ans=n;
for(int i=1;i<=n;i++){//暴力去重
int ju=0;
for(int j=1;j<i;j++){
k=0;
cmp(i,j,1,kk,1,kk);
if(k==0) ju=1;
}
ans=ans-ju;
}
printf("%d\n",ans);
}
return 0;
}