(HNU_ACM) Sequence

Source: Internet
Author: User

//////////////////////////////////////// ////////////////////////

I wrote an article for the first time and got a simple question.

Problem description

Giving a collection S of points on two dimension plane. (S = {(x0, y0), (x1, y1 ),...}) we define a point is greater than another point when all its coordinate on two axis are both greater than or equel to another one. namely, p is greater than q when xp ≥xq and yp ≥yq. A sequence is a list points <p1, p2,...> satisfy that I <j => pi is greater than pj. you can use the elements in S to constru Ct sequences, how many sequences needed to cover a S at least?

 
Input

The input consists of several test cases. each test case start with a line containing a number n (0 <n ≤1000000), the number of points in S. then n lines follows, each line containing two number, xi, yi (0 ≤ xi, yi ≤ 100000), the position of point I. the input end with EOF.

 
Output

You have to print minium Number of sequences needed to cover s in a single line for each case.

 
Sample Input
4
1 1
2 2
3 3
4

4
1 5
2 6
2 3
3 4
 
Sample output
1
2

/*************************************** **************************************** *************************************

To give a set S = {(x0, y0), (x1, Y1 )........... (Xn, yn)}, the condition for element S1> S2 in the set is: x0> = x1 & y0> = Y1.

It is required to find some descending subsequences in s so that all the elements of these subsequences can overwrite all the elements in S, and these subsequences have the least number.

2007-10-4

From acm.hun.cn

**************************************** **************************************** *************************************/

/*************************************** **************************************** *************************************
Solution:

Use greedy algorithms.

First, sort S by X, and then sort by Y by Level 2 (it doesn't matter if the order is ascending or descending ).

Assume that the first descending subsequence A1 is initialized to {<y0, y0 >}, A1 can use the original S;

Start from the second element in sequence. For each element, modify the first sub-sequence that can be modified (that is, yi in S is smaller than yn in An). Otherwise, start a sub-sequence Aj.

This principle is to use greed to keep the greatest potential and the minimum modification potential.

For more information, see the program.
**************************************** **************************************** *************************************/

The procedure is as follows:

# Include <stdio. h>
# Include <stdlib. h>

# Deprecision Max 1000010
Typedef struct
{
Long;
Long B;
} Vri;
Vri p [Max];

// S in the question

// Comparison function in the quick rank

Int cmp (const vri * x, const vri * y)
{
If (x-> a> y-> a) return 1;
If (x-> a <y-> a) return-1;
Return x-> B> = y-> B? 1:-1;
Return 0;
}
Int main ()
{
Long I, j, k, num;
While (1)
{
If (scanf ("% ld", & num) = EOF) break;
For (I = 0; I <num; I ++)
Scanf ("% ld", & p [I]. a, & p [I]. B );
Qsort (p, num, sizeof (p [0]), cmp); // qsort
P [0]. a = p [0]. B; // The first subsequence
For (j = 1, I = 1; I <num; I ++) // If j is set to 1, it indicates that there is already a subsequence. a is used to save the last item of the subsequence.
{
For (k = 0; k <j; k ++) // you can find and modify the subsequence
If (P [I]. B> = P [K]. A) // P [I]. B> = the last entry in the subsequence can be modified.
{
P [K]. A = P [I]. B; // save the last modified
Break;
}
If (k> = J) P [J ++]. A = P [I]. B; // If you cannot find any of the preceding values, you can create another column.
}
Printf ("% LD/N", J); // The Last J is the number of subsequences.
}
Return 0;
}

Note: In this question, I use ascending search, which is somewhat different from the question, but it doesn't matter.

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.