我真的是心态崩了,今晚是什么情况,十分钟敲完代码,只对了1个case,我以为是判断最大还是最小堆有问题,一直找,一直改,还是不行,后来去网上看了别人的代码,改了先遍历整个堆确定是什么堆,再后序遍历一次输出,结果还是只对一个???当时我就黑人问号脸,最后发现,"Not Heap"少打了个't',鼓捣了40分钟???最后我把第一遍的代码交了一遍AC了。WTF!!!
下次再这种情况,倒立喝水。
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int m,n,type;
void dfs(int pos, int pre_val, int flag, vector<int> t, vector<int> &post){
if(pos > n) return;
int tmp = 0;
if(pre_val > t[pos]) tmp = 1;
if(pre_val < t[pos]) tmp = -1;
if(tmp != flag && flag != 0){
type = 0;
}
// cout << t[pos] << " tmp: " << tmp << " flag: " << flag << " type: "<< type<< endl;
dfs(pos*2,t[pos],tmp,t,post);
dfs(pos*2+1,t[pos],tmp,t,post);
post.push_back(t[pos]);
}
int main(){
scanf("%d%d",&m,&n);
while(m-- > 0){
vector<int> t;
t.resize(n+1);
for(int i = 1; i <= n; ++i){
scanf("%d",&t[i]);
}
if(t[1] > t[2] || t[1] > t[3]) type = 1;
else if(t[1] < t[2] || t[1] < t[3]) type = -1;
vector<int> post_order;
dfs(1,t[1],0,t,post_order);
if(type == 1)
printf("Max Heap\n");
else if(type == -1)
printf("Min Heap\n");
else
printf("Not Heap\n");
for(int j = 0; j < post_order.size(); ++j){
if(j != 0)
printf(" %d",post_order[j]);
else
printf("%d",post_order[j]);
}
printf("\n");
}
return 0;
}