Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present

题目链接
在这里插入图片描述
思路:变相求最长上升子序列求路径。

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e3+5;
struct node{
	int x,y,id;
};
vector<node>v;
int deep=0,dp[maxn],pre[maxn];
bool cmp(const node &a,const node &b)
{
	return (a.x==b.x)?a.y<b.y:a.x<b.x;
}
void dfs(int x)
{
	if(dp[x]==1) {
		printf("%d ",v[x].id);return ;
	}
	dfs(pre[x]);
	printf("%d ",v[x].id);
}
int main()
{
	int n,w,h,x,y,ans=5001;
	dp[ans]=0;
	scanf("%d %d %d",&n,&w,&h);
	for(int i=1;i<=n;++i)
	{
		scanf("%d %d",&x,&y);
		if(x>w&&y>h) v.push_back({x,y,i});
	}
	sort(v.begin(),v.end(),cmp);
	for(int i=0;i<v.size();++i)
	{
		dp[i]=1;
		for(int j=0;j<i;++j)
		if(v[i].x>v[j].x&&v[i].y>v[j].y&&dp[j]+1>dp[i])
		dp[i]=dp[j]+1,pre[i]=j;
		if(dp[i]>dp[ans]) ans=i;
	}
	if(dp[ans]==0){
		printf("0\n");return 0;
	}
	printf("%d\n",dp[ans]);
	dfs(ans);
}
发布了328 篇原创文章 · 获赞 1 · 访问量 9080

猜你喜欢

转载自blog.csdn.net/qq_42479630/article/details/105280027