7-11 关于堆的判断 (25 分) (建堆)
要学会建堆,然后知道用数组模拟堆的优点。
#include <iostream> #include <cstring> #include <map> using namespace std; const int N = 1010; int heap[N]; int n, m; void insert(int pos, int x) // 建堆 { heap[pos] = x; while(pos > 1) { if(heap[pos] < heap[pos >> 1]) { swap(heap[pos], heap[pos >> 1]); pos >>= 1; } else { break; } } } int main() { cin >> n >> m; map<int,int>p; // 用map 来 记录每个节点对应在数组中的下标 for(int i = 1; i <= n; i++) { int x; cin >> x; insert(i, x); } for(int i = 1; i <= n; i++) p[heap[i]] = i; while(m--) { string s; int x, y, flag = 0; cin >> x >> s; if(s == "and") { // 根据每句话的特性来判断。 我觉得这非常妙 cin >> y >> s >> s; if(heap[p[x] ^ 1] == y) flag = 1; } else { cin >> s; if(s == "a") { cin >> s >> s >> y; if(heap[p[x] >> 1] == y) flag = 1; } else { cin >> s; if(s == "root") { if(heap[1] == x) flag = 1; } else { cin >> s >> y; if(heap[p[y] >> 1] == x) flag = 1; } } } if(flag) cout << "T" << endl; else cout << "F" << endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App