数据过淼
查看原帖
数据过淼
886055
MoonCake2011楼主2023/7/31 17:12

时间复杂度为 O(nt)\text O(nt) 的能过。

#include<bits/stdc++.h>
using namespace std;
int n,t;
int dp[int(1e6)+10];
struct node{
	int l,r;
}a[2500010];
bool cmp(node x,node y){
	if(x.r!=y.r) return x.r<y.r;
	return x.l<y.l;
}
int main() {
	cin>>n>>t;
	memset(dp,0x3f,sizeof dp);
	for(int i=1;i<=n;i++){
		cin>>a[i].l>>a[i].r;
		if(a[i].l==1) dp[a[i].r]=1;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=t;i++)
		for(int j=a[i].l-1;j<a[i].r;j++)
			dp[a[i].r]=min(dp[a[i].r],dp[j]+1);
	if(dp[t]==0x3f3f3f3f) cout<<-1;
	else cout<<dp[t];
	return 0;
}
2023/7/31 17:12
加载中...