zoukankan      html  css  js  c++  java
  • 1155 Heap Paths (30分)

    #include<iostream>
    #include<vector>
    
    using namespace std;
    
    const int N = 1010;
    
    int heap[N];
    int n, flag;
    int isMax, isMin;
    vector<int> ans;
    
    void dfs(int u){
        ans.push_back(heap[u]);
        int l = u * 2, r = u * 2 + 1;
        if(l > n){
            cout << ans[0];
            for(int i = 1; i < ans.size(); i ++){
                cout << ' ';
                if(ans[i] > ans[i - 1]) isMin = 1;
                if(ans[i] < ans[i - 1]) isMax = 1;
                cout << ans[i];
            }
            puts("");
            return;
        }
        
        if(r <= n){
            dfs(r);
            ans.pop_back();
        } 
        
        if(l <= n){
            dfs(l);
            ans.pop_back();
        }
        
    }
    
    int main(){
        cin >> n;
        
        for(int i = 1; i <= n; i ++) cin >> heap[i];
        
        dfs(1);
        
        if(isMax && isMin) puts("Not Heap");
        else if(isMax) puts("Max Heap");
        else puts("Min Heap");
        
        return 0;
    }
    

    第四个测试点到底是啥?????
    应该是判断的时候错了
    wa代码

    #include<iostream>
    #include<vector>
    
    using namespace std;
    
    #define LL long long
    
    const int N = 1010;
    
    LL heap[N];
    int n, flag;
    LL k;
    vector<LL> ans;
    
    void dfs(int u){
        ans.push_back(heap[u]);
        int l = u * 2, r = u * 2 + 1;
        if(l > n){
            if(ans.size() > 1) k = ans[0] - ans[1];
            for(int i = 0; i < ans.size(); i ++){
                cout << ans[i] << (i == ans.size() - 1 ? "
    " : " ");
                if(i >= 2 && (ans[i] - ans[i - 1]) * (ans[i - 1] - ans[i - 2]) < 0) flag = 1;
            }
            
            return;
        }
        
        if(r <= n){
            dfs(r);
            ans.pop_back();
        } 
        
        if(l <= n){
            dfs(l);
            ans.pop_back();
        }
        
    }
    
    int main(){
        cin >> n;
        
        for(int i = 1; i <= n; i ++) cin >> heap[i];
        
        dfs(1);
        
        if(flag) puts("Not Heap");
        else if(k > 0) puts("Max Heap");
        else puts("Min Heap");
        
        return 0;
    }
    
  • 相关阅读:
    Iterator与 Enumeration
    多态性理解
    django---路由层
    django常用模块汇总
    django初识
    python常见模块
    python PEP8规范
    logging模块
    mysql一些了解的名词
    python 链接 mysql数据库
  • 原文地址:https://www.cnblogs.com/tomori/p/13711698.html
Copyright © 2011-2022 走看看