1462F - The Treasure of The Segments

1462F - The Treasure of The Segments

题意: 有n条线段,给出每条线段的左右端点,现在要找一个集合,集合中至少有一个线段是与其他所有线段都有交点的,问要找出这样的集合至少要在原来的n条线段中去除多少条

n ≤ 2 e 5 n \leq 2e5 n2e5

思路: 这个n刚开始就没想到可以枚举(

枚举那个特殊的线段(就是与其他都有交点的那个),如果一条线段与这条线段没有交点则只有两种情况:1,线段的右端点小于特殊线段的左端点 2,线段左端点大于特殊线段右端点

放到两个数组里面二分,判断这步就是 O ( l o g n ) O(logn) O(logn)

code

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;

struct node {
    
    
	int l, r;
}  mp[N];

vector<int> ls, rs;

inline void init() {
    
    
	ls.clear(); rs.clear();
} 

int main() {
    
    
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int T;
	cin >> T;
	while(T--) {
    
    
		init();
		int n;
		cin >> n;
		for(int i = 0; i < n; ++ i) {
    
    
			cin >> mp[i].l >> mp[i].r;
			ls.push_back(mp[i].l);
			rs.push_back(mp[i].r);
		}
		sort(ls.begin(), ls.end());
		sort(rs.begin(), rs.end());
		int ans = 0;
		for(int i = 0; i < n; ++ i) {
    
    
			int L = mp[i].l;
			int cnt = lower_bound(rs.begin(), rs.end(), L) - rs.begin();
			int R = mp[i].r;
			int cnt2 = upper_bound(ls.begin(), ls.end(), R) - ls.begin();
			ans = max(ans, cnt2 - cnt);
		}
		
		cout << n - ans << endl;
	}
} 

猜你喜欢

转载自blog.csdn.net/qq_39602052/article/details/113905489