HDU -- (1025) constructing roads in jgshining's kingdom (DP/LIS + binary)

Source: Internet
Author: User
Tags define local
Constructing roads in jgshining's kingdom

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


Problem descriptionjgshining'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. ^_^

 

Inputeach 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.

 

Outputfor 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 input21 22 131 22 33 1

 

Sample outputcase 1: My king, at most 1 road can be built. Case 2: My king, at most 2 roads can be built. HintHuge input, scanf is recommended.

 

Authorjgshining (Aurora shadow) indicates that each vertex corresponds to a vertex. There is no point to multiple vertices or multiple vertices, and there cannot be intersection lines. Ask the points that you connect to make the most lines. Given a B, it is not difficult to construct the ascending order given by STR [a] = B, where A is 1-n, so we only need to perform monotonically incrementing subsequence processing on the values in the STR [a] array .... simple and clear code:
 1 //#define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 const int maxn=50005; 5 const int inf=0x3f3f3f3f; 6 int str[maxn],dp[maxn],sac[maxn]; 7 int res; 8  9 int binary(int v,int n)10 {11     int ll=1,rr=n,mid;12     while(ll<=rr)13     {14       mid=ll+(rr-ll)/2;15       if(sac[mid]<=v&&sac[mid]!=-1)16         ll=mid+1;17       else18         rr=mid-1;19     }20     return ll;21 }22 void LIS(int n)23 {24       res=1;25 //    for(int i=1;i<=n;i++)26 //      sac[i]=inf;27     memset(sac,-1,sizeof(int)*(n+1));28    for(int i=1;i<=n;i++)29    {30        dp[i]=binary(str[i],res);31        if(res<dp[i])32            res=dp[i];33        if(str[i]<sac[dp[i]]||sac[dp[i]]==-1)34            sac[dp[i]]=str[i];35    }36 }37 int main()38 {39   #ifdef LOCAL40   freopen("test.in","r",stdin);41   #endif42 43   int n,i,p,r,ct=1;44   while(scanf("%d",&n)!=EOF)45   {46       for( i=1;i<=n;i++){47       scanf("%d%d",&p,&r);48         str[p]=r;49       }50         LIS(n);51       printf("Case %d:\n",ct++);52       if(res==1)53        printf("My king, at most 1 road can be built.\n");54     else55        printf("My king, at most %d roads can be built.\n",res);56     printf("\n");57   }58   return 0;59 }
View code

 

HDU -- (1025) constructing roads in jgshining's kingdom (DP/LIS + binary)

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.