Ural1078(经典dp)
来源:互联网 发布:零基础学php书 编辑:程序博客网 时间:2024/06/07 00:28
1078. Segments
Time limit: 1.0 second
Memory limit: 64 MB
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 toN (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 areN 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
4-2 2-1 1-3 34 5
32 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 0
- Ural1078(经典dp)
- 经典DP
- DP经典5题 【DP】
- poj 1163经典DP
- poj2385-dp经典
- ZOJ1499经典dp
- poj 1191 dp经典
- 经典DP 嵌套矩形
- HDU2196 经典树形DP
- DP经典5题
- POJ 1185 经典dp
- 区间dp经典 poj2955
- hdu 1421 经典DP
- poj1160post_office经典dp
- HDOJ 1069(经典Dp)
- 邮局--dp经典问题
- The Triangle 经典DP
- 【DP经典问题】WordBreak
- C中的野指针—如何避免
- 【坑】使用<!DOCTYPE html>的时候多个CSS可以不用逗号分隔
- hdu 1228
- tomcat7源码编译过程以及问题解决
- svm详细总结
- Ural1078(经典dp)
- 徐居正 四佳亭
- Ncurses介绍
- hdoj.1673 Optimal Parking 20141012
- 新的起点,新的开始!
- 在使用viewPager做图片切换时注意图片存放的位置
- String类及其常用操作方法
- Kettle 5.x使用指南
- 三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)