acwing算法基础之贪心--区间问题和Huffman树

123 篇文章 1 订阅

1 基础知识

暂无。。。

2 模板

暂无。。。

3 工程化

题目1:区间选点。给你N个区间,让你在数轴上选一些点,要求N个区间中至少有一个点被选出。求选一些点的最少数目。

解题思路:贪心,按照右端点排序,每次选择右端点,维护一个res和右端点right即刻,如果当前区间左端点大于right,则更新rightres自增。

C++代码如下,

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<pair<int,int>> nums;
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        nums.emplace_back(a,b);
    }
    
    sort(nums.begin(), nums.end(), [](const pair<int,int> a, const pair<int,int> b) {
        return a.second < b.second;
    });
    
    int res = 0;
    int right = -2e9;
    for (auto [x,y] : nums) {
        if (x > right) {
            res += 1;
            right = y;
        }
    }
    
    cout << res << endl;
    
    return 0;
}

题目2:求最大不相交区间数。给定N个区间,你可以选择一些区间,要求这些区间不能相交,求区间的最大数目。

解题思路:同题目1的解题思路。

C++代码如下,

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<pair<int,int>> nums;
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        nums.emplace_back(a,b);
    }
    
    sort(nums.begin(), nums.end(), [](const pair<int,int> a, const pair<int,int> b) {
        return a.second < b.second;
    });
    
    int res = 0;
    int right = -2e9;
    for (auto [l, r] : nums) {
        if (l > right) {
            res += 1;
            right = r;
        }
    }
    
    cout << res << endl;
    
    return 0;
}

题目3:给你N个区间,请将它们分成若干组,要求每组内部两两区间互不相交,求组数目的最小值。

关键步骤介绍如下:

  1. 将区间按照左端点排序。
  2. 定义一个小根堆heap,用来存储每个组中所有区间右端点的最大值。
  3. 遍历每个区间[l, r],如果堆顶大于等于l,则将r插入到堆中;否则弹出堆顶,再将r插入到堆中。
  4. 最终答案等价于堆中的元素数目。

C++代码如下,

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<pair<int,int>> nums;
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        nums.emplace_back(a,b);
    }
    
    sort(nums.begin(), nums.end());
    
    priority_queue<int, vector<int>, greater<int>> heap;
    for (auto [l, r] : nums) {
        if (heap.empty() || heap.top() >= l) {
            heap.push(r);
        } else {
            heap.pop();
            heap.push(r);
        }
    }
    
    cout << heap.size() << endl;
    
    return 0;
}

题目4:给定N个区间和一个[s,t],求能把[s,t]完全覆盖住的最少区间数目。

解题关键步骤:

  1. 将所有区间按左端点进行排序。
  2. 定义变量int start = stint res = 0
  3. 遍历每一个区间:定义变量int r = -2e9,在所有能覆盖住start的区间中(即nums[j].first <= start),选择右端点最大的区间(即r = max(r, nums[j].second))。如果r < start,说明无法覆盖住,终止循环break。然后res += 1start = r,如果start >= ed,提前终止循环,返回答案res

C++代码如下,

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int st, ed;
    cin >> st >> ed;
    
    int n;
    cin >> n;
    
    vector<pair<int,int>> nums;
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        nums.emplace_back(a,b);
    }
    
    //按照左端点排序
    sort(nums.begin(), nums.end(), [](const pair<int,int> a, const pair<int,int> b) {
        return a.first < b.first;   
    });
    
    int res = 0;
    int start = st;
    bool is_succ = false;
    for (int i = 0; i < nums.size(); ++i) {
        int r = -2e9;
        int j = i;
        while (j < nums.size() && nums[j].first <= start) {
            r = max(r, nums[j].second);
            j += 1;
        }
        
        if (r < start) { //最左侧的区间的左端点都大于start,无法覆盖住
            break;
        }
        
        res += 1;
        start = r;
        
        if (start >= ed) {
            is_succ = true;
            break;
        }
        
        i = j - 1;
    }
    
    if (is_succ) cout << res << endl;
    else cout << -1 << endl;
    
    return 0;
}

题目5:合并果子。给你N堆不同数量的果子,每次可以选择任意两堆进行合并,合并代价和那两堆果子的数目之和。求将N堆果子合并成一堆果子的最小代价。

解题思路:贪心做法,每次选择最小的两堆进行合并。

C++代码为,

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;
    
    priority_queue<int, vector<int>, greater<int>> heap;
    while (n--) {
        int x;
        cin >> x;
        heap.push(x);
    }
    
    int res = 0;
    while (heap.size() > 1) {
        int a = heap.top(); heap.pop();
        int b = heap.top(); heap.pop();
        res += a + b;
        heap.push(a + b);
    }
    
    cout << res << endl;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值