http://acm.hdu.edu.cn/showproblem.php?pid=1025
Constructing Roads in jgshining ' s kingdom
problem Descriptionjgshining ' s kingdom consists of 2n (n is no more than 500,000) small cities which be located in both parallel lines.
half of these cities is rich in resource (we call them rich cities) while the others is short for resource (we call T Hem 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 Excludi Ng Rich 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 CO Nstructing 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, p Lease 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 Input21 22 131 22 33 1
Sample OutputCase 1:my King, at the most 1 road can be built. Case 2:my King, at the most 2 roads can be built.
HintHuge input, scanf is recommended.Test Instructions: The input has two sides, for P,q, all p to Q does not intersect the maximum number of edges can be connected, then as long as from 1 to N enumeration p, and then road[p] is a Q, so road[p] lis is good. idea: The complexity of direct LIS is O (n^2), for 500000 of the data is definitely tle, plus the two-point optimization complexity can be reduced to O (Nlogn).
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5 using namespacestd;6 #defineN 5000057 8 intDp[n],road[n];9 intx;Ten One voidCalinta) A { - intL=1, r=X,mid; - while(l<=R) { theMid= (l+r) >>1; - if(dp[mid]<a) l=mid+1; - Elser=mid-1; - } +dp[l]=A; - } + A intMain () at { - intN; - intcas=0; - while(~SCANF ("%d",&N)) { -Memset (DP,0,sizeof(DP)); -x=1; in for(intI=1; i<=n;i++){ - intb; toscanf"%d%d",&a,&b); +road[a]=b; - } thedp[1]=road[1]; * for(intI=2; i<=n;i++){ $ intA=Road[i];Panax Notoginseng if(A>dp[x]) dp[++x]=A; - ElseCal (a); the } +printf"Case %d:\n",++CAs); A if(x==1) printf ("My King, at most 1 road can be built.\n\n"); the Elseprintf"My King, at the most%d roads can built.\n\n", x); + } - return 0; $}
HDU 1025:constructing Roads in jgshining ' s kingdom (lis+ two-point optimization)