1155 Heap Paths (30 分)

这道题不知道哪出了错误有一个测试点过不了

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 int a[1100];
 5 int n;
 6 vector<vector<int>> sp;
 7 void dfs(int index, vector<int> p)
 8 {
 9 
10     if (2 * index + 2 <n)
11     {
12         p.push_back(a[2 * index + 2]);
13         dfs(2 * index + 2, p);
14         p.pop_back();
15     }
16     if (2 * index + 1 < n)
17     {
18         p.push_back(a[2 * index + 1]);
19         dfs(2 * index + 1, p);
20         p.pop_back();
21     }
22     if (2 * index + 1 >= n && 2 * index + 2 >= n){
23         //cout << a[index]<<endl;
24         sp.push_back(p);
25         return;
26     }
27 }
28 int main()
29 {
30 
31     cin >> n;
32     for (int i = 0; i < n; i++)
33     {
34         int x = 0;
35         cin >> x; a[i] = x;
36     }
37     vector<int> p; p.push_back(a[0]);
38     dfs(0, p);
39     bool is = true; bool may = true;
40     for (int i = 0; i < sp.size(); i++)
41     {
42         int temp = sp[i][0]; int size = 0;int equal=0;
43         for (int j = 0; j < sp[i].size(); j++)
44         {
45            if(j>0 && temp==sp[i][j]){equal++;temp=sp[i][j];}
46             if (j>0 && temp > sp[i][j]){ size++; temp = sp[i][j];may = false; }
47             if (j>0 && temp <sp[i][j]){ size--; temp = sp[i][j]; }
48             printf("%d", sp[i][j]);
49             if (j != sp[i].size() - 1)printf(" ");
50         }
51         if (abs(size)+equal != sp[i].size() - 1){ is = false; }
52         printf("\n");
53     }
54     if (is){
55         if (!may)printf("Max Heap");
56         else printf("Min Heap");
57     }
58     else printf("Not Heap");
59     return 0;
60 }

 

posted @ 2018-12-10 21:18  代码拯救世界可以吗  阅读(396)  评论(0编辑  收藏  举报