标签:fst str bool owb 证明 思路 space std cto
题意:
最多能选几条不重叠的线段
思路:
按R从小到大排序,维护一个最大的右端点
右端点最小的那个线段是必选的,可以贪心地证明
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<stack> #include<queue> #include<deque> #include<set> #include<vector> #include<map> #include<functional> #define fst first #define sc second #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lc root<<1 #define rc root<<1|1 #define lowbit(x) ((x)&(-x)) using namespace std; typedef double db; typedef long double ldb; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> PI; typedef pair<ll,ll> PLL; const db eps = 1e-6; const int mod = 1e9+7; const int maxn = 2e6+100; const int maxm = 2e6+100; const int inf = 0x3f3f3f3f; int n; struct node{ int x, y; }a[maxn]; bool cmp(node a, node b){ //if(a.y==b.y)return a.x<b.x; return a.y<b.y; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d%d", &a[i].x,&a[i].y); } sort(a+1,a+1+n,cmp); int ans = 1; int r = a[1].y; for(int i = 1; i <= n; i++){ if(a[i].x>=r){ ans++; r = a[i].y; } } printf("%d",ans); return 0; }
51nod 1133 不重叠的线段 (贪心,序列上的区间问题)
标签:fst str bool owb 证明 思路 space std cto
原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/10441097.html