Hdu1025constructing Roads in jgshining ' s kingdom (Lis Max Ascending subsequence)

Source: Internet
Author: User
Tags sort
constructing Roads in jgshining ' s kingdom Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 24704 Accepted Submission (s): 7038


Problem Description jgshining ' kingdom consists of 2n (n is no more than 500,000) small cities which be located in both PA Rallel 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. ^_^

Input each 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.

Output for 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
2 1 2 2 1 3 1 2 2 3 3 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.
Author jgshining (Aurora Dazzle)
The main topic: There are two lines, each line has n points, the number of the n points is 1~n, but the position is not in accordance with the order of 1~n, and now to be numbered the same points between the lines, but to meet the line between no cross, ask the most even how many lines.
Problem: The topic at first glance is a dichotomy, but think about it we can sort a line before converting it into a bare LIS problem code as follows:
#include <iostream> #include <bits/stdc++.h> using namespace std; struct Node {int A, B;}

NO[500005];

BOOL CMP (node A1,node a2) {return a1.a<a2.a;}

int dp[500005];
    int binsearch (int le,int aim) {int l=1;
    int r=le;
        while (l!=r) {int mid= (L+R)/2;
        if (Dp[mid]>=aim) R=mid;
    else l=mid+1;
} return L;
    } int LIS (node* a,int le) {memset (dp,0,sizeof (DP));
    dp[1]=a[1].b;
    int now=1;
        for (int i=1;i<=le;i++) {int c=binsearch (NOW,A[I].B);
        dp[c]=a[i].b;
            if (c==now&&dp[c]<a[i].b) {now+=1;
        dp[now]=a[i].b;

    } else dp[c]=a[i].b;
} return now;
    } int main () {int n;
    int case1=0;
        while (scanf ("%d", &n) ==1) {case1++;
        for (int i=1;i<=n;i++) scanf ("%d%d", &no[i].a,&no[i].b);
        Sort (no+1,no+n+1,cmp); int Ans=lis (NO,N);
        printf ("Case%d:\n", case1);
        if (ans==1) printf ("My King, at the most 1 road can be built.");
        else printf ("My King, at the most%d roads can be built.", ans);
        printf ("\ n");
    cout<<ans<<endl;
    }//cout << "Hello world!" << Endl;
return 0;
 }


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.