Constructing Roads in jgshining ' s kingdom
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 16956 Accepted Submission (s): 4819
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.HintHuge input, scanf is recommended. Test instructions: The topic gives 2n cities, distributed on both sides of the road, one side for the rich city, the other for the poor city, through the development of the Times, now to establish the road between rich and poor cities connected, asked to meet the conditions of the Maximum number of roads. (Conditions: Road 22 does not intersect, and is in line with the corresponding conditions); problem: Simple DP, is a longest ascending sub-sequence of the topic, but the problem is very pit, in the final output there is also to consider whether the number of roads is greater than 1, if greater than 1, you need to output roads. This pit has been a long time Look at the topic to be careful, see clearly, understand the problem after the hands to play code. AC Code:#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < queue> #include <map> #include <vector> #include <cmath> #include <cctype>using namespace std ; typedef long long ll;const int M = 5e5 + 100;const int INF = 0xffffff;int D[m];char s[] = "s"; struct node{int x, y; BOOL operator < (const Node &a) const {return a.y > y; } void Readin () {scanf ("%d%d", &x,&y); }}p[m];int Main () {freopen ("in", "R", stdin); int n,cnt = 0; while (~SCANF ("%d", &n)) {int to = 0; for (int i = 0; i < n; i++) P[i].readin (); Sort (p,p + N); for (int i = 0; i < n; i++) {int u = lower_bound (d,d + to,p[i].x)-D; if (U = = to) d[to++] = p[i].x; else d[u] = p[i].x; } printf ("Case%d:\n", ++cnt); printf ("My King, at the most%d Road%scan is built.\n\n", To,to > 1? s: ""); } return 0;}
Constructing Roads in jgshining ' s kingdom (longest ascending subsequence)