【集训Day3】section

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值