1 给定很多线段,每个线段都有两个数[start, end],
表示线段开始位置和结束位置,左右都是闭区间
规定:
1)线段的开始和结束位置一定都是整数值
2)线段重合区域的长度必须>=1
返回线段最多重合区域中,包含了几条线段
方法一:枚举所有区间上每个“.5”包含的线段,比较求出最大值
方法二:将线段按开始位置由小到大排序,再建立一个小根堆,将小根堆内部小于开始位置的数全弹出,此时小根堆有几个数就重合几条线段,并将该线段结尾加入小根堆
意义在于以每条线段为左边界时,会有几条线段越过他往外穿,因为最大重合区域一定是有某一个左边界
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int maxcover1(vector<vector<int>>& lines)
{
int minval = INT_MAX;
int maxval = INT_MIN;
for (int i = 0; i < lines.size(); i++)
{
minval = min(minval, lines[i][0]);
maxval = max(maxval, lines[i][1]);
}
int cover = 0;
for (double p = minval + 0.5; p < maxval; p+=1)
{
int cur = 0;
for (int i = 0; i < lines.size(); i++)
{
if (lines[i][0]<p && lines[i][1]>p)
cur++;
}
cover = max(cover, cur);
}
return cover;
}
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct line {
int start;
int end;
line(int s, int e) :start(s), end(e) {};
};
struct linecompare {
bool operator()(line& a, line& b)
{
return a.start < b.start;
}
};
int maxcover2(vector<vector<int>>& m)
{
vector<line> lines(m.size());
for (int i = 0; i < m.size(); i++)
{
lines[i] = line(m[i][0], m[i][1]);
}
sort(lines.begin(), lines.end(), linecompare());
priority_queue<int, vector<int>, greater<int>> heap;
int maxval = 0;
for (int i = 0; i < m.size(); i++)
{
while (!heap.empty() && heap.top() <= lines[i].start)
{
heap.pop();
}
heap.push(lines[i].end);
maxval = max(maxval, (int)heap.size());
}
return maxval;
}
2加强堆的实现
系统提供堆的弊端:已经入堆的元素,若排序
#include <iostream>
#include <vector>
#include <unordered_map>
#include <functional>
#include <algorithm>
using namespace std;
template <typename T>
class HeapGreater {
private:
vector<T> heap;
unordered_map<T, int> indexMap;
int heapSize;
function<bool(const T&, const T&)> comp;
public:
HeapGreater(function<bool(const T&, const T&)> c) : heapSize(0), comp(c) {}
bool isEmpty() {
return heapSize == 0;
}
int size() {
return heapSize;
}
bool contains(T obj) {
return indexMap.find(obj) != indexMap.end();
}
T peek() {
return heap[0];
}
void push(T obj) {
heap.push_back(obj);
indexMap[obj] = heapSize;
heapInsert(heapSize++);
}
T pop() {
T ans = heap[0];
swap(0, heapSize - 1);
indexMap.erase(ans);
heap.pop_back();
heapSize--;
heapify(0);//往下做调整
return ans;
}
void remove(T obj)/*先将最后一个元素取出值,然后删除,再检查要删除的是否为最后一个*/ {
T replace = heap[heapSize - 1];
int index = indexMap[obj];
indexMap.erase(obj);
heap.pop_back();
heapSize--;
if (obj != replace) {
heap[index] = replace;
indexMap[replace] = index;
resign(replace);
}
}
void resign(T obj) //某个数发生变化后继续调好堆
{
int index = indexMap[obj];
heapInsert(index);
heapify(index);//只会发生一个
}
vector<T> getAllElements() {
return heap;
}
private:
void heapInsert(int index) {
while (index > 0 && comp(heap[index], heap[(index - 1) / 2])) {
swap(index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
void heapify(int index) {
int left = index * 2 + 1;
while (left < heapSize) {
int best = left + 1 < heapSize && comp(heap[left + 1], heap[left]) ? left + 1 : left;
best = comp(heap[best], heap[index]) ? best : index;
if (best == index) {
break;
}
swap(best, index);
index = best;
left = index * 2 + 1;
}
}
void swap(int i, int j) {
T temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
indexMap[heap[i]] = i;
indexMap[heap[j]] = j;//反向索引表同步换
}
};
int main() {
// Example usage of HeapGreater class
auto compare = [](const int& a, const int& b) { return a > b; };
HeapGreater<int> heap(compare);
heap.push(3);
heap.push(1);
heap.push(2);
cout << heap.pop() << endl; // Output: 1
cout << heap.pop() << endl; // Output: 2
cout << heap.pop() << endl; // Output: 3
return 0;
}
指标发生变化,系统提供的堆做不到o(logn)级别调整 系统提供的堆只能弹出顶部元素,不能自由删除元素,要建立反向索引表