码迷,mamicode.com
首页 > 其他好文 > 详细

【暴力】vijos P1897 学姐吃牛排

时间:2014-11-01 08:37:15      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   for   sp   div   log   

判断堆:递归判断每个节点的孩子是否都比其父亲大(小)。

判断BST:中序遍历是否有序。

 1 #include<cstdio>
 2 using namespace std;
 3 #define lc (rt<<1)
 4 #define rc (rt<<1|1)
 5 int T,n,a[1001],b[1001],en;
 6 void Mid(int rt)
 7 {
 8     if(lc<=n) Mid(lc);
 9     b[++en]=a[rt];
10     if(rc<=n) Mid(rc);
11 }
12 bool is_BST()
13 {
14     bool f1=1,f2=1; en=0; Mid(1);
15     for(int i=2;i<=n;i++) if(b[i]<b[i-1]) {f1=0; break;}
16     if(f1) return 1;
17     for(int i=2;i<=n;i++) if(b[i]>b[i-1]) {f2=0; break;}
18     if(f2) return 1;
19     return 0;
20 }
21 bool is_Heap(int rt)
22 {
23     if(lc<=n)
24       {
25           if(a[lc]>a[rt]) return 0;
26           if(!is_Heap(lc)) return 0;
27       }
28     if(rc<=n)
29       {
30           if(a[rc]>a[rt]) return 0;
31           if(!is_Heap(rc)) return 0;
32       }
33     return 1;
34 }
35 bool is_Heap_2(int rt)
36 {
37     if(lc<=n)
38       {
39           if(a[lc]<a[rt]) return 0;
40           if(!is_Heap_2(lc)) return 0;
41       }
42     if(rc<=n)
43       {
44           if(a[rc]<a[rt]) return 0;
45           if(!is_Heap_2(rc)) return 0;
46       }
47     return 1;
48 }
49 int main()
50 {
51     scanf("%d",&T);
52     for(int i=1;i<=T;i++)
53       {
54           printf("Case #%d: ",i);
55           scanf("%d",&n);
56           for(int j=1;j<=n;j++) scanf("%d",&a[j]);
57           bool flag1=0,flag2=0;
58           if(is_BST()) flag1=1;
59           if(is_Heap(1)) flag2=1;
60           if((!flag2)) if(is_Heap_2(1)) flag2=1;
61           if(flag1&&flag2) puts("Both");
62           else if(flag1) puts("BST");
63           else if(flag2) puts("Heap");
64           else puts("Neither");
65       }
66     return 0;
67 }

 

【暴力】vijos P1897 学姐吃牛排

标签:style   blog   io   color   os   for   sp   div   log   

原文地址:http://www.cnblogs.com/autsky-jadek/p/4066361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!