Hangzhou Electric HDU ACM 1025 constructing Roads in jgshining ' s kingdom

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): 17732 Accepted Submission (s): 5023


Problem Descriptionjgshining ' s kingdom consists of 2n (n is no further than 500,000) small cities which is located in both par Allel lines.

Half of these cities is rich in resource (we call them rich cities) while the others is short for resource (we call them Poor cities). Each of the poor city are short of exactly one kind of resource and also each of the rich city are rich in exactly one kind of resource. Assume no poor cities is short of one same kind of resource and no, rich cities is rich in one same kind of resource.

With the development of industry, poor cities wanna import resource from rich ones. The roads existed is so small this they ' re unable to ensure the heavy trucks, so new roads should is built. The poor cities strongly BS each and so is 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 Othe R rich ones. Because of economic benefit, any rich city would be willing to export resource to any poor one.

Rich Citis marked from 1 to n is located in line I and poor ones marked from 1 to n is located in line Ii.

The location of the Rich City 1 are on the left of any other cities, and the rich City 2 are on the left of any other cities excluding R Ich city 1, Rich City 3 are on the right of the 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, the crossed roads may cause a lot of traffic accident so jgshining have established a law to forbid constr Ucting crossed roads.

For example, the roads in Figure I is forbidden.



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

Inputeach test case would begin with a line containing an integer n (1≤n≤500,000). then n lines follow. Each line contains the integers p and r which represents that Poor city p needs to the import resources from the Rich City R Proc ESS to the end of file.

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

Sample Input
21 22 131 22 33 1

Sample Output
Case 1:my King, at the most 1 road can be built. Case 2:my King, at the most 2 roads can be built. Hint Huge input, scanf is recommended.

Authorjgshining (Aurora Dazzle) ThisThe longest ascending subsequence (LIS) length of the O (NLOGN) algorithm learning process, drawing on others blog article algorithm introduction :Http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html

In the Sichuan OJ encountered a problem can not be used n^2 too, all kinds of entanglements, the last acquisition of NLOGN algorithm

The longest increment subsequence, longest increasing subsequence below we précis-writers for LIS.
The sort +lcs algorithm and the DP algorithm are ignored, both of which are too easy to understand.

Suppose there is a sequence d[1..9] = 2 1 5 3 6 4 8 9 7, it can be seen that the LIS length is 5. N
Try to find it step by step below.
We define a sequence B and then let i = 1 to 9 examine the sequence one at a.
In addition, we use a variable len to record the maximum number of times now.

First, put d[1] in order B, make b[1] = 2, that is, when there are only 11 digits 2, the smallest end of the LIS with a length of 1 is 2. Then Len=1

Then, put d[2] in an orderly place in B, so that b[1] = 1, that is, the minimum length of the LIS is 1,d[1]=2 is useless, it is easy to understand it. Then Len=1

Next, d[3] = 5,d[3]>b[1], so make b[1+1]=b[2]=d[3]=5, that is, the minimum end of the LIS with a length of 2 is 5, it is easy to understand. This time b[1..2] = 1, 5,len=2

Again, d[4] = 3, it just add to 1, 5, placed in the position of 1 is obviously inappropriate, because 1 is less than 3, the minimum length of the LIS is 1, so it is easy to infer that the length of the LIS min 1 is 2, so you can eliminate 3, this time b[1..2] = 5, 3,len = 1

Continue, d[5] = 6, it is behind 3, because b[2] = 3, and 6 is behind 3, so it is easy to infer b[3] = 6, then b[1..3] = 1, 3, 6, or is it easy to understand? Len = 3, OH.

6th, D[6] = 4, you see it between 3 and 6, so we can replace 6, get b[3] = 4. B[1..3] = 1, 3, 4, Len continues to be equal to 3

7th one, d[7] = 8, it's big, bigger than 4, uh. So b[4] = 8. Len becomes 4.

8th, D[8] = 9, get b[5] = 9, uh. Len continues to grow, to 5.

The last one, d[9] = 7, which is between b[3] = 4 and b[4] = 8, so we know that the latest b[4] =7,b[1..5] = 1, 3, 4, 7, 9,len = 5.

So we know the length of the LIS is 5.

!!!!! Attention. This 1,3,4,7,9 is not the LIS, it just stores the corresponding length to the minimum end of the LIS. With this at the end, we can insert data one at a-one place. Although the last d[9] = 7 update is not meaningful for this set of data, but if there are two numbers 8 and 9, then you can update 8 to d[5], 9 update to d[6], the length of the LIS is 6.

Then you should find one thing: inserting data in B is ordered and is replaced without moving--that is, we can use a binary search to optimize the insertion time of each number to O (logn) ~~~~~ The time complexity of the algorithm is reduced to O (NLOGN)!

Popular understanding is easy, so to understand, with the latter element to replace the DP "" is larger than this element, this process to ensure that Len is constant, that is, the longest increment of the subsequence is unchanged, then replaced with the smaller element, is to make

Further elements are more "opportunity" to add to the DP "" at the end of the 1, in other words, the potential for longer sequences increases.

Also pay attention to the output format details. Road and roads. Also note that the end of the binary search must be low above high and per "low". Y is greater than the number you are looking for

AC:

#include <iostream> #include <stdio.h> #include <algorithm>const int m=500000+10;using namespace STD; struct city{int x, y;} Per[m];bool CMP (city a,city b) {return a.x<b.x;}    int main () {int n,dp[m],t=0;        while (Cin>>n) {for (int i=0;i<n;i++) scanf ("%d%d", &per[i].x,&per[i].y);        Sort (per,per+n,cmp);        int Len=1,low,high;        DP[1]=PER[0].Y;            for (int j=1;j<n;j++) {if (Per[j].y>dp[len]) dp[++len]=per[j].y;                    else {Low=1;high=len;                        while (low<=high) {int mid= (Low+high)/2;                        if (Per[j].y>dp[mid]) low=mid+1;                    else high=mid-1;            } dp[low]=per[j].y; }} if (len==1) cout<< "Case" <<++t<< ": \ n" << "MY King, at the most "<<len<<" road can be built. "        <<endl; else cout<< "Case" <<++t<< ": \ n" << "My King, at the most" <<len<< "Roads can bu" Ilt. "        <<endl;    cout<< "\ n"; } return 0;}


Hangzhou Electric HDU ACM 1025 constructing Roads in jgshining ' s kingdom

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.