section

解题思路
先将左端点排序,对右端点做最长不下降子序列。
code
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int n;
int b[100010],tot;
struct abc{
int x,y;
}a[100010];
int cmp(abc a,abc b)
{
if(a.x!=b.x)
return a.x<b.x;
return a.y>b.y;
}
int fd(int x)
{
int l=1,r=tot,s;
while(l<=r)
{
int mid=(l+r)/2;
if(b[mid]<x)
r=mid-1,s=mid;
else
l=mid+1;
}
return s;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1,cmp);
b[++tot]=a[1].y;
for(int i=2;i<=n;i++)
{
if(a[i].y<=b[tot])
b[++tot]=a[i].y;
else
b[fd(a[i].y)]=a[i].y;
}
cout<<tot<<endl;
}

本文介绍了一种通过动态规划解决最优化问题的方法,通过先对左端点进行排序,然后对右端点找到最长不下降子序列,应用于数据结构和算法场景中。代码实现展示了如何利用比较函数和查找函数来高效计算结果。
537

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



