题解:AT_arc126_b [ARC126B] Cross-free Matching
题意
有两行,一行
思路
为了方便描述,第
显然,两条相交的线
那么我们对
这样我们只需要对
代码
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
constexpr int N = 1e6 + 10;
struct A{int x, y;}a[N];
int f[N], tot, n, m;
int find(int x){
int l = 0, r = tot;
while(l < r){
int mid = (l + r) >> 1;
if(f[mid] < x)l = mid + 1;
else r = mid;
}
return l;
}
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++)
cin >> a[i].x >> a[i].y;
sort(a + 1, a + 1 + m, [](A&i, A&j){return (i.x ^ j.x) ? (i.x < j.x) : (i.y > j.y);});
f[tot = 1] = a[1].y;
for(int i = 1; i <= m; i++){
if(a[i].y > f[tot])f[++tot] = a[i].y;
else f[find(a[i].y)] = a[i].y;
}
cout << tot << endl;
return 0;
}