E - 7
思路:
我也讲不清楚,看的大佬的代码,模拟了一下确实非常有道理
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 1e9 + 7;
ll n, m;
struct Point{
int x, y;
bool operator<(const Point & B)const{
return 1ll * x * B.y > 1ll * y * B.x;
}
};
struct seg{
Point a, b;
bool operator<(const seg &B)const{
return a < B.a;
}
};
void work()
{
cin >> n;
vector <seg> v(n);
for(int i = 0; i < n; ++i){
int x, y;
cin >> x >> y;
Point a = Point{x - 1, y};
Point b = Point{x, y - 1};
v[i] = seg{a, b};
}
sort(v.begin(), v.end());
int ans = 1;
Point last = v[0].a;
for(int i = 1; i < n; ++i){
if(v[i].b < last) continue;
last = v[i].a;
++ans;
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}

这篇博客探讨了一种二维平面上线段排序的算法,通过比较线段起点来排序,并解决线段之间的覆盖问题。代码实现中,定义了结构体Point和seg,用于表示线段的起点和线段本身,然后使用排序和遍历的方法找出不被其他线段覆盖的线段数量。这是一个典型的几何问题与数据结构结合的应用。
421

被折叠的 条评论
为什么被折叠?



