#include <bits/stdc++.h> using namespace std; int findMinArrowShots(vector<vector<int>>& points) { priority_queue<int, vector<int>, greater<int>> heap; int ans = 0; int n = points.size(); sort(points.begin(), points.end(), [](const vector<int>& x1, const vector<int>& x2) { return x1[0] < x2[0]; // 按照气球的开始位置排序 }); for (int i = 0; i < n; ++i) { while (!heap.empty() && heap.top() < points[i][0]) { // 检查当前箭是否能够射中当前气球 heap.pop(); } heap.push(points[i][1]); // 改为存储气球的结束位置 ans = max(ans, (int)heap.size()); } return ans; } int main() { vector<vector<int>> points; int n; cin >> n; points.resize(n, vector<int>(2)); // 分配内存空间 for (int i = 0; i < n; i++) { cin >> points[i][0] >> points[i][1]; } int ans = findMinArrowShots(points); cout<<ans<<endl; return 0; }
import java.util.*; public class Main { /** * @param lines 二维数组,行数表示线段个数,列有两列,第一列表示start,第二列表示end * @return 最大重叠线段数 */ public static int maxCover(int[][] lines) { //开始排序 Arrays.sort(lines, (a, b) -> (a[0] - b[0])); //默认为小根堆 PriorityQueue<Integer> heap = new PriorityQueue<>(); int max = -1; for (int[] line : lines) { while (!heap.isEmpty() && heap.peek() < line[0]) { heap.poll(); } heap.add(line[1]); max = Math.max(max, heap.size()); } return max; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[][] lines = new int[n][2]; for (int i = 0; i < n; i++) { lines[i][0] = input.nextInt(); lines[i][1] = input.nextInt(); } System.out.println(maxCover(lines)); } }
import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int time = sc.nextInt(); int[][] times = new int[time][2]; for(int i=0;i<time;i++){ times[i][0] = sc.nextInt(); times[i][1] = sc.nextInt(); } Arrays.sort(times, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[0]-o2[0]; } }); PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[1]-o2[1]; } }); int maxSize = 1; queue.add(times[0]); for(int i=1;i<times.length;i++){ while(!queue.isEmpty()){ int[] peek = queue.peek(); if(peek[1]<times[i][0]){ queue.poll(); }else{ break; } } queue.add(times[i]); maxSize = Math.max(maxSize,queue.size()); } System.out.println(maxSize); } }