Ural 1112,lis

Source: Internet
Author: User

Title Link: http://acm.timus.ru/problem.aspx?space=1&num=1112

Test instructions: n segment, to take some, so that the left segment of any segment is not in the interior of a segment.

In fact, it is to take the least number of line segments, so that does not coincide.

Data volume is very small, 100, direct Lis O (n^2) Engage.

First press X from small to large row, then, press X to do the LIS with the previous line of the y ratio, and then record the precursor is OK.

Then output, just beginning to prepare the recursive output, think of the next, did not come out, on the violence and saved again, in fact, can be recursive output, as long as the first one to find a line segment, where the first line is not determined, the use of an ANS variable, see how many layers to recursion.

#include <bits/stdc++.h>using namespacestd;#defineMAXN 105structline{intx, y;} LINES[MAXN];BOOLCMP (line A,line b) {returna.x<b.x;}intDP[MAXN];intPREV[MAXN];intAns =0;voidPrint (intPOS) {    if(ans!=1)    {        --ans;    Print (Prev[pos]); } printf ("%d%d\n", LINES[POS].X,LINES[POS].Y);}intMain () {memset (DP,0,sizeof(DP)); intN; scanf ("%d",&N);  for(intI=0; i<n; i++)    {        intx, y; scanf ("%d%d",&x,&y); if(x>y) Swap (x, y); lines[i].x= X,LINES[I].Y =y; } sort (Lines,lines+n,cmp); dp[0] =1;  for(intI=1; i<n; i++)    {        intK =0; intpos =-1;  for(intj =0; j<i; J + +)        {            if(lines[j].y<=lines[i].x&&k<Dp[j]) {k=Dp[j]; POS=J; }} Dp[i]= k +1; if(pos!=-1) Prev[i]=POS; } ans=0; intpos =-1;  for(intI=0; i<n; i++)    {        if(ans<Dp[i]) {ans=Dp[i]; POS=i; }} printf ("%d\n", ans); /*vector<line> Vaj;            for (int i=0; i<ans; i++) {vaj.push_back (Lines[pos]);            printf ("%d%d\n", lines[pos].x,lines[pos].y);        pos = Prev[pos];    } for (int i = ans-1;i>=0;i--) printf ("%d%d\n", vaj[i].x,vaj[i].y); */print (POS); return 0;}

Ural 1112,lis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.