Hdu1025 longest rising sub-sequence (nlogn algorithm) DP

Source: Internet
Author: User

Constructing roads in jgshining's kingdom

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3938 accepted submission (s): 1119


Problem description

Jgshining's kingdom consists of 2n (N is no more than 500,000) small cities which are located in two parallel lines.

Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities ). each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. you may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.

With the development of industry, poor cities wanna import resource from rich ones. the roads existed are so small that they're unable to ensure the heavy trucks, so new roads shoshould be built. the poor cities strongly BS each other, so are the rich ones. poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of Road with other rich ones. because of economics benefit, any rich city will be willing to export resource to any poor one.

Rich citis marked from 1 to n are located in line I and poor ones marked from 1 to n are located in line II.

The location of rich city 1 is on the left of all other cities, rich city 2 is on the left of all other cities excluding rich city 1, rich City 3 is on the right of rich city 1 and rich city 2 but on the left of all other cities... and so as the poor ones.

But as you know, two crossed roads may cause a lot of traffic accident so jgshining has established a law to forbid constructing crossed roads.

For example, the roads in Figure I are forbidden.

 

In order to build as your roads as possible, the young and handsome king of the kingdom-jgshining needs your help, please help him. ^_^

 

 

Input

Each test case will begin with a line containing an integer N (1 ≤ n ≤ 500,000 ). then n lines follow. each line contains two integers P and R which represents that poor city p needs to import resources from rich city R. process to the end of file.

 

 

Output

For each test case, output the result in the form of sample.
You shoshould tell jgshining what's the maximal number of road (s) can be built.

 

 

Sample Input

2

1 2

2 1

3

1 2

2 3

3 1

 

 

Sample output

Case 1:

My king, at most 1 road can be built.

 

Case 2:

My king, at most 2 roads can be built.

 

 

 

Beginner
DP,

No idea after thinking for a long time
,

Baidu
,

See the longest incrementing subsequence.
,

Bytes
?

The complexity of the longest incrementing subsequence is not
O (N ^ 2)

?
, 500000

How can this problem be solved?
?!

Then I found myself stupid.
B

Now
,

There were
O (nlgn)

Algorithm
. Wa

Once
,

When there were more than one path
Road

Use the plural
,

Fainted
......

 

The Code is as follows:
:

 

 

 # Include <stdio. h> <br/> # include <string. h> <br/> # define maxn 500005 <br/> int poor [maxn], Res [maxn], Len [maxn]; <br/> int find (int, int L, int R) <br/>{< br/> int mid = (L + r)/2; <br/> while (L <= r) <br/>{< br/> if (a = res [Mid]) return mid; <br/> else <br/> {<br/> if (a> res [Mid]) L = Mid + 1; <br/> else r = mid-1; <br/> mid = (L + r)/2; <br/>}< br/> return l; <br/>}< br/> int main () <br/>{< br/> int I, K, N, C As, Max, R, Pos; <br/> CAS = 1; <br/> while (scanf ("% d", & N )! = EOF) <br/>{< br/> memset (Res, 0, sizeof (RES); <br/> memset (Len, 0, sizeof (LEN )); <br/> for (I = 1; I <= N; ++ I) RES [I] = maxn; <br/> for (I = 1; I <= N; ++ I) <br/>{< br/> scanf ("% d", & K, & R ); <br/> poor [k] = r; <br/>}< br/> res [1] = poor [1]; <br/> Len [1] = 1; <br/> for (I = 2; I <= N; ++ I) <br/> {<br/> Pos = find (poor [I], 1, n); <br/> res [POS] = poor [I]; <br/> Len [I] = Pos; <br/>}< br/> max = 0; <br/> for (I = 1; I <= N; ++ I) if (LEN [I]> MAX) max = Len [I]; <br/> If (max = 1) printf ("case % d: /nmy king, at most % d road can be built. /n ", CAS ++, max); <br/> else printf (" case % d:/nmy king, at most % d roads can be built. /n ", CAS ++, max); <br/>}< br/> return 0; <br/>}< br/>

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.