1、团队程序设计天梯赛-练习集-L2-012 关于堆的判断
解题思路:
关键是如何建小顶堆,要一边输入一边建堆才可以(边输入边建堆和最后统一建堆的顺序是不一定是一样的)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 1e3 + 10;
int H[maxn];
int N, M;
map<int, int> Map;
char s_t[20];
void Adjust(int pos, int n);
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; ++i) {
scanf("%d", &H[i]);
Adjust(i, i);
}
for (int i = 1; i <= N; ++i) {
Map[H[i]] = i;
}
for (int i = 0; i < M; ++i) {
int a, b;
int id1, id2;
scanf("%d", &a);
id1 = Map[a];
scanf("%s", s_t);
if (s_t[0] == 'a') {
scanf("%d", &b);
scanf("%s%s", s_t, s_t);
id2 = Map[b];
if (id1 != id2 && (id1/2) == (id2/2)) {
printf("T\n");
} else {
printf("F\n");
}
} else {
scanf("%s", s_t);
if (s_t[0] == 'a') {
scanf("%s%s", s_t, s_t);
scanf("%d", &b);
id2 = Map[b];
if (id1 != id2 && id1/2 == id2) {
printf("T\n");
} else {
printf("F\n");
}
} else {
scanf("%s", s_t);
if (s_t[0] == 'r') {
if (id1 == 1) {
printf("T\n");
} else {
printf("F\n");
}
} else {
scanf("%s", s_t);
scanf("%d", &b);
id2 = Map[b];
if (id1 != id2 && (id2/2) == id1) {
printf("T\n");
} else {
printf("F\n");
}
}
}
}
}
return 0;
}
void Adjust(int pos, int n)
{
while (pos >= 2) {
int x = pos / 2;
int Left = 2*x;
int Right = 2*x + 1;
if (Right <= n) {
if (H[Right] < H[Left]) {
if (H[Right] < H[x]) {
swap(H[Right], H[x]);
}
} else if (H[Left] < H[Right]) {
if (H[Left] < H[x]) {
swap(H[Left], H[x]);
}
}
} else if (Left <= n) {
if (H[Left] < H[x]) {
swap(H[Left], H[x]);
}
}
--pos;
}
}
2、实现堆结构
定义一个数组,初始化为空。在数组上执行两种操作:
1、增添1个元素,把1个新的元素放入数组。
2、输出并删除数组中最小的数。
使用堆结构实现上述功能的高效算法。
输入
第一行输入一个整数t,代表测试数据的组数。
对于每组测试数据,第一行输入一个整数n,代表操作的次数。
每次操作首先输入一个整数type。
当type=1,增添操作,接着输入一个整数u,代表要插入的元素。
当type=2,输出删除操作,输出并删除数组中最小的元素。
1<=n<=100000。
输出
每次删除操作输出被删除的数字。
样例输入
2
5
1 1
1 2
1 3
2
2
4
1 5
1 1
1 7
2
样例输出
1
2
1
提示
每组测试数据的复杂度为O(nlgn)的算法才能通过本次,否则会返回TLE(超时)
需要使用最小堆结构来实现本题的算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
struct cmp {
bool operator() (int &a, int &b) {
return a > b;
}
};
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int t;
scanf("%d", &t);
while (t--) {
priority_queue<int, vector<int>, cmp> pQueue;
int n;
scanf("%d", &n);
int type, u;
while (n--) {
scanf("%d", &type);
if (type == 1) {
scanf("%d", &u);
pQueue.push(u);
} else {
printf("%d\n", pQueue.top());
pQueue.pop();
}
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
int Size;
int num[maxn];
void Insert(int key);
void Delete(void);
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int t;
scanf("%d", &t);
while (t--) {
Size = 0;
int n;
scanf("%d", &n);
int type, u;
while (n--) {
scanf("%d", &type);
if (type == 1) {
scanf("%d", &u);
Insert(u);
} else {
Delete();
}
}
}
return 0;
}
void Insert(int key)
{
num[++Size] = key;
int pos = Size;
while (pos > 1 && num[pos] < num[pos/2]) {
swap(num[pos/2], num[pos]);
pos /= 2;
}
}
void Delete(void)
{
printf("%d\n", num[1]);
num[1] = num[Size--];
int pos = 2;
int pos_pre = 1;
while (pos <= Size) {
if (pos <= Size-1 && num[pos+1] < num[pos]) {
++pos;
}
if (num[pos] < num[pos_pre]) {
swap(num[pos], num[pos_pre]);
pos_pre = pos;
pos <<= 1;
} else {
break;
}
}
}