Ural1078(经典dp)

1078. Segments

Time limit: 1.0 second
Memory limit: 64 MB
A number of segments are lying on a line. Every segment is given with the coordinates of its endpoints. Segments are numbered from 1 to N (0 <  N < 500). We assume, that one segment is inside another, if the two segments are different, the first one is fully contained in the second one, and their endpoints do not coincide. Write a program, which finds the numbers of the segments in the longest sequence of segments which are contained in. In the sequence, every segment except the last is inside the next segment in the sequence.

Input

The first line contains one integer N. Next, there are N lines, with two integers on every line, which are the coordinates of the left and the right endpoints of the corresponding segment. These coordinatesare integers in the interval [–10000, 10000]. We assume that, the given segments are numbered according to their place in the input.

Output

The first line must contain one integer, equal to the number of segments in the found sequence. The following line must contain the numbers of the segments in this sequence. These numbers must be outputted, in the order in which the segments' lengths increase, starting from the smallest.If there are more than one output sequences, write any of them.

Sample

inputoutput
4
-2 2
-1 1
-3 3
4 5
3
2 1 3



闲来无事,来写一发。。。

这是一道经典的dp题,题意就是有n条线段,线段之间如果x1<x2 && y2<y1 就是线段1包含线段2,求n个线段中最多能包含几条线段,然后还要打印出路径,就是从最短的开始打印。和矩形嵌套是一样一样滴~~~

这里要首先排一发序,然后再dp.

//求包含的线段的最多个数,类似于矩形的包含那道题 ,dp算法 
// 还需要打印路径 。。 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define maxn 510 
int  d[maxn];//代表以i为最外层的 组合数  
int pre[maxn];
int ans[maxn];
struct Edge
{
	int id;
	int x,y;
}edge[maxn];

bool cmp(Edge a,Edge b)
{
	if(a.x==b.x )return a.y<b.y;
	else return a.x>b.x;
}

int main()
{
//	freopen("q.in","r",stdin);
	int n;
	int i,j,cnt=0;
	memset(d,0,sizeof(d));
	memset(pre,-1,sizeof(pre));
	memset(ans,0,sizeof(ans));
	
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d%d",&edge[i].x,&edge[i].y);
		edge[i].id=i+1;
	}
		
	sort(edge,edge+n,cmp);
//	for(i=0;i<n;i++)cout<<edge[i].id<<" ";
//	cout<<endl;

    for(i=0;i<n;i++)
    {
    	for(j=0;j<i;j++)
    	{
    		if(edge[i].x<edge[j].x && edge[i].y>edge[j].y  && d[i]<d[j]+1) //这里都不能有等号,y端明确说明,但是x端没有明确说明。 
    		{
    			d[i]=d[j]+1;
    			pre[i]=j;
    		}
    	}
    	if(!d[i])d[i]=1;
    //	cout<<d[i]<<" * "; 
    }
   // cout<<endl;
    
    int mmax=d[0],mmaxi=0;
    for(i=1;i<n;i++)
	{
		if(d[i]>mmax)
		{
			mmax=d[i];
			mmaxi=i; 
		}
	} 
	cout<<mmax<<endl;

	//cout<<cnt<<endl;
	ans[cnt++]=edge[mmaxi].id;
	for(i=pre[mmaxi];i!=-1;i=pre[i])
	{
		ans[cnt++]=edge[i].id;
	}
	cout<<ans[cnt-1];cnt-=2;// 这里注意一下 
	while(cnt>=0)
	{
		cout<<" "<<ans[cnt];
		cnt--;
	}
	
	cout<<endl;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值