P1090 合并果子

P1090 合并果子

题目入口:https://www.luogu.org/problem/P1090

一、优先队列AC代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <functional>
using namespace std;
inline const int read(){
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9'){ if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9'){ x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}
priority_queue<int, vector<int>, greater<int> > q;
int main(){
    int n, num;
    n = read();
    for (int i = 1; i <= n; i++){
        num = read();
        q.push(num);
    }
    int a, b, ret = 0;
    while (q.size() >= 2){
        a = q.top();
        q.pop();
        b = q.top();
        q.pop();
        ret += a+b;
        q.push(a+b);
    }
    printf("%d\n", ret);
    return 0;
}

这里用了一下堆排序
发现有点难受
因为堆排序的排序规则与其他堆操作都是相反的
而堆排序规则又要保持高度一致…
不过利用vector的下标也算是巧妙的实现了出来
然而不出所料的TLE了…
因为每次都要跑sort_heap…
罢了罢了 还是优先队列大法好

二、排列堆TLE代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
using namespace std;
inline const int read(){
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9'){ if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9'){ x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}
vector<int> v;
int main(){
    int n = read(), num;
    for (int i = 0; i < n; i++){
        num = read();
        v.push_back(num);
    }
    make_heap(v.begin(), v.end(), less<int>()); //建立堆 排序方式为大顶堆
    sort_heap(v.begin(), v.end(), less<int>()); //这里很有意思 sort的结果反而是小顶堆
    int a, b, ret = 0, t = 1;
    while (t < n){
        a = v[0+2*(t-1)];
        b = v[1+2*(t-1)];
        ret += a+b;
        v.push_back(a+b);
        make_heap(v.begin()+2*t, v.end(), less<int>()); //建立堆 排序方式为大顶堆
        sort_heap(v.begin()+2*t, v.end(), less<int>()); //这里很有意思 sort的结果反而是小顶堆
        t++;
    }
    printf("%d\n", ret);
    return 0;
}

参考网站:https://blog.csdn.net/c20182030/article/details/52727101
看到一个博客的解法
这个堆排序用的妙啊…
使用了数组的动态抹除
利用了数组的下标定位排序范围

三、堆排序AC代码

#include <iostream>
#include <cstdio>
#include <functional>   //greater
#include <algorithm>    //heap
using namespace std;
inline const int read(){
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9'){ if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9'){ x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}
const int MAXN = 1e4+10;
int heap[MAXN];
int size = 0;
void Put(int num){
    heap[size++] = num;
    push_heap(heap, heap+size, greater<int>());
}
int Get(){
    pop_heap(heap, heap+size, greater<int>());
    return heap[--size];
}
int main(){
    int n = read(), num;
    for (int i = 0; i < n; i++){
        num = read();
        Put(num);
    }
    int a, b, ret = 0;
    for (int i = 1; i < n; i++){
        a = Get();
        b = Get();
        Put(a+b);
        ret += a+b;
    }
    printf("%d\n", ret);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值