一、一对数(l,r)排序(java,c++)
Java:如例题所示
题目:
给定 N 个闭区间 [ai,bi,请你将这些区间分成若干组,使得每组内部的区间两两之间(包括端点)没有交集,并使得组数尽可能小。
输出最小组数。
输入格式
第一行包含整数 N,表示区间数。
接下来 N 行,每行包含两个整数 ai,bi,表示一个区间的两个端点。
输出格式
输出一个整数,表示最小组数。
import java.util.*;
import java.io.*;
public class Main{
static class Edge{
int l;
int r;
public Edge(int l,int r){
this.l = l;
this.r = r;
}
}
static List<Edge> edges;
static int n;
static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException{
String[] s = cin.readLine().split("\\s+");
n = Integer.parseInt(s[0]);
edges = new ArrayList<>();
for(int i = 0; i < n; i++){
String[] s1 = cin.readLine().split("\\s+");
int l = Integer.parseInt(s1[0]);
int r = Integer.parseInt(s1[1]);
edges.add(new Edge(l, r));
}
//按左端点进行排
edges.sort((e1, e2)-> e1.l - e2.l);
//通过minHeap维护每组的尾端
Queue<Integer> minheap = new PriorityQueue<Integer>();
for(int i = 0; i < n; ++i){
int l = edges.get(i).l;
int r = edges.get(i).r;
//判断:任意组中右端点的最小值的组 >= 当前区间的的左端点L:新组
if(minheap.isEmpty() || minheap.peek() >= l){
minheap.add(r);
}else{
//可加入到老组中update 老组的 max r
int max_r = minheap.poll();
minheap.add(Math.max(max_r, r));
}
}
System.out.print(minheap.size());
}
}
C++:operator重写
下例是升序
struct Range
{
int l,r;
bool operator< (const Range &W)const
{
return r<W.r;
}
}ranges[N];
二、堆的用法
Java:PriorityQueue
默认情况下,PriorityQueue
创建的是小顶堆,但是可以通过传递一个自定义的比较器来创建大顶堆。
import java.util.*;
public class Main {
public static void main(String[] args) {
// 创建小顶堆
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
minHeap.add(3);
minHeap.add(1);
minHeap.add(2);
System.out.println("Min heap:");
while (!minHeap.isEmpty()) {
System.out.print(minHeap.poll() + " ");
}
System.out.println();
// 创建大顶堆
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
maxHeap.add(3);
maxHeap.add(1);
maxHeap.add(2);
System.out.println("Max heap:");
while (!maxHeap.isEmpty()) {
System.out.print(maxHeap.poll() + " ");
}
System.out.println();
}
}
C++:priority_queue
默认情况下,std::priority_queue
创建的是大顶堆,但是可以通过传递一个自定义的比较器来创建小顶堆。
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
// 创建小顶堆
priority_queue<int, vector<int>, greater<int>> minHeap;
minHeap.push(3);
minHeap.push(1);
minHeap.push(2);
cout << "Min heap:" << endl;
while (!minHeap.empty()) {
cout << minHeap.top() << " ";
minHeap.pop();
}
cout << endl;
// 创建大顶堆
priority_queue<int> maxHeap;
maxHeap.push(3);
maxHeap.push(1);
maxHeap.push(2);
cout << "Max heap:" << endl;
while (!maxHeap.empty()) {
cout << maxHeap.top() << " ";
maxHeap.pop();
}
cout << endl;
return 0;
}
三、键值对
c++:map
map<int, int> stu;
stu.insert(pair<int, int>(i, j));
auto it_find = stu.find(f);//it_find是迭代器:map<int,int>::iterator
if (it_find!=stu.end()) {.....}
JAVA:HashMap
import java.util.HashMap; // 引入 HashMap 类
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Runoob");
Sites.put(3, "Taobao");
Sites.put(4, "Zhihu");
//使用get()方法来通过key访问value
System.out.println(Sites.get(3));
//删除元素
Sites.remove(4);
//删除所有键值对(key-value)可以使用 clear 方法:
Sites.clear();
//如果要计算 HashMap 中的元素数量可以使用 size() 方法:
//迭代遍历
// 输出 key 和 value
for (Integer i : Sites.keySet()) {
System.out.println("key: " + i + " value: " + Sites.get(i));
}
// 返回所有 value 值
for(String value: Sites.values()) {
// 输出每一个value
System.out.print(value + ", ");
}