数据结构-堆

priority_queue就是系统自带的堆,前期尽量手写,熟练后可以使用priority_queue

堆排序

//堆排序
#include<bits/stdc++.h>
using namespace std;
int n;
int heap[100010], len;
inline void up(int x){
    while (x > 1 && heap[x] < heap[x / 2]){
        swap(heap[x], heap[x / 2]);
        x /= 2;
    }
}
inline void down(int x){
    while (2 * x <= len){
        int j = 2 * x;
        if (j + 1 <= len && heap[j + 1] < heap[j]){
            j++;
        }
        if (heap[x] <= heap[j]){
            break;
        }
        swap(heap[x], heap[j]);
        x = j;
    }
}

int main(){
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        int x;
        scanf("%d", &x);
        heap[++len] = x;
        up(len);
    }
    for (int i = 1; i <= n; i++){
        printf("%d ", heap[1]);
        swap(heap[1], heap[len--]);
        down(1);
    }
} 

合并数列

//合并数列
#include<bits/stdc++.h>
using namespace std;
int n, m, len;
struct node{
    int v, k;
}heap[100010];
inline void up(int k){
    while (k > 1 && heap[k].v < heap[k / 2].v){
        swap(heap[k], heap[k / 2]);
        k /= 2;
    }
}
inline void down(int k){
    while (2 * k <= len){
        int j = 2 * k;
        if (j + 1 <= len && heap[j + 1].v < heap[j].v){
            j++;
        }
        if (heap[k].v <= heap[j].v){
            break;
        }
        swap(heap[k], heap[j]);
        k = j;
    }
}

int main(){
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        int x, y;
        scanf("%d%d", &x, &y);
        heap[++len].v = y;
        heap[len].k = x;
        up(len);
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; i++){
        node y = heap[1];
        printf("%d ", y);
        swap(heap[1], heap[len--]);
        down(1);
        heap[++len].v = y.v + y.k;
        heap[len].k = y.k;
        up(len);
    }
} 

大富翁游戏

//大富翁游戏
#include<bits/stdc++.h>
using namespace std;
int n, m, len1, len2;
struct node{
    int v, pos;
};
int c1[100010], c2[100010]; // 第i个人所在的节点
node heap1[100010], heap2[100010];
inline void up1(int k){
    while (k > 1 && heap1[k].v < heap1[k / 2].v){
        swap(heap1[k], heap1[k / 2]);
        c1[heap1[k].pos] = k;
        c1[heap1[k / 2].pos] = k / 2;
        k /= 2;
    }
}
inline void down1(int k){
    while (2 * k <= len1){
        int j = 2 * k;
        if (j + 1 <= len1 && heap1[j + 1].v < heap1[j].v){
            j++;
        }
        if (heap1[k].v <= heap1[j].v){
            break;
        }
        swap(heap1[k], heap1[j]);
        c1[heap1[k].pos] = k;
        c1[heap1[j].pos] = j;
        k = j;
    }
}
inline void up2(int k){
    while (k > 1 && heap2[k].v > heap2[k / 2].v){
        swap(heap2[k], heap2[k / 2]);
        c2[heap2[k].pos] = k;
        c2[heap2[k / 2].pos] = k / 2;
        k /= 2;
    }
}
inline void down2(int k){
    while (2 * k <= len2){
        int j = 2 * k;
        if (j + 1 <= len2 && heap2[j + 1].v > heap2[j].v){
            j++;
        }
        if (heap2[k].v >= heap2[j].v){
            break;
        }
        swap(heap2[k], heap2[j]);
        c2[heap2[k].pos] = k;
        c2[heap2[j].pos] = j;
        k = j;
    }
}

int main(){
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++){
        heap1[++len1].v = 100;
        heap1[len1].pos = i;
        c1[i] = len1;
        heap2[++len2].v = 100;
        heap2[len2].pos = i;
        c2[i] = len2;
    }
    for (int i = 1; i <= m; i++){
        int x;
        scanf("%d", &x);
        if (x == 1){
            int a, b;
            scanf("%d%d", &a, &b);
            heap1[c1[a]].v += b;
            up1(c1[a]), down1(c1[a]);
            heap2[c2[a]].v += b;
            up2(c2[a]), down2(c2[a]);
        }
        else if (x == 2){ 
            printf("%d %d\n", heap2[1], heap1[1]);
        }
    }
} 

动态中位数

//动态中位数
#include<bits/stdc++.h>
using namespace std;
priority_queue<int> q1;
priority_queue<int, vector<int>, greater<int>> q2;
int n;  

int main(){
    scanf("%d", &n);
    int x;
    scanf("%d", &x);
    q1.push(x);
    for (int i = 1; i <= (n - 1) / 2; i++){
        printf("%d ", q1.top());
        int a, b;
        scanf("%d%d", &a, &b);
        if (a < q1.top() && b < q1.top()){
            q1.push(a);
            q1.push(b);
            q2.push(q1.top());
            q1.pop();
        }
        else if (a >= q1.top() && b >= q1.top()){
            q2.push(a);
            q2.push(b);
            q1.push(q2.top());
            q2.pop();
        }
        else if (a < q1.top() && b >= q1.top()){
            q1.push(a);
            q2.push(b);
        }
        else if (a >= q1.top() && b < q1.top()){
            q1.push(b);
            q2.push(a);
        }
    }
    printf("%d", q1.top());
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值