Original title:
-
Problem Description
…….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. -
Input
Each 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.
-
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 most 1 road can be built.
(Empty line!!!)
Case 2:
My king, at most 2 roads can be built.
Ideas for problem solving:
- Pay attention to the output format!!! road and roads!!!
- To tell you the truth, I have read this question for a long time, and the meaning of the question is understood (for a long time * 10000). (The meaning of the question on the Internet is miscellaneous and difficult to understand, collapse ing). This question should be so understood, in order to understand how to solve the problem:
- Enter a number n to indicate that poor and rich have n cities
- Enter two digits X and Y in each line to represent the resources of a poor city with coordinates x and rich city with coordinates y, respectively.
- The coordinates of all cities of poor and rich increase in turn from left to right, that is, the coordinates of the two cities from left to right are 1, 2, 3,... ... n
- Then we should connect the two cities of p and r that need each other, without crossing, and figure out how many roads can be connected, that is, how many roads can be built.
- Take a classic example:
- 5
- p: 2 3 4 1 5
- c: 2 3 1 4 5
- If the coordinates are connected, it is easy to see that up to three lines can be connected (22, 33, 55).
- p: 1 2 3 4 5
- | | |
- c: 1 2 3 4 5
- 1 and 4, 4 and 1 intersect each other.
- So at this time, there should be a train of thought. We C a n solve the problem by using a n array a[n] to store the coordinates of each P city and the corresponding C city, a [i] = value: I represents the coordinates of p, value represents the coordinates of c, so that the coordinates of P city are equivalent to those of C already sorted. Now we only need to get the longest incremental subsequence of array a, so that we c a n surely get it. The results do not cross (p is ordered, i.e. the subscript to array a), and they are the best results.
Code:
Longest incremental subsequence optimization algorithm: O (nlogn)
#include <stdio.h>
#include <iostream>
using namespace std;
int dp[500001], temp[500001];
int BinarySearch(int tail, int findValue)//Two points search
{
int head = 1;
int mid = (head + tail) / 2;
while (mid != 1 && mid != tail)
{
if (dp[mid] == findValue)return mid;
if (dp[mid] > findValue)
tail = mid;
else
head = mid;
mid = (head + tail) / 2;
if (head + 1 == tail && dp[mid] < findValue)
return mid + 1;
}
if (mid == 1 && dp[mid] > findValue)return 1;
else return 2;
}
int main()
{
int temp_p, temp_r, i, dpIndex;
int n, Case = 0;
while (scanf("%d", &n) != EOF)
{
Case++;
for (i = 1; i <= n; i++)
{
scanf("%d%d", &temp_p, &temp_r);
temp[temp_p] = temp_r;
}
dp[1] = temp[1];
dpIndex = 1;
for(i = 2; i <= n; i++)
{
if (temp[i] > dp[dpIndex])
dp[++dpIndex] = temp[i];
else
dp[BinarySearch(dpIndex, temp[i])] = temp[i];
}
if(dpIndex == 1)
{
printf("Case %d:\n", Case);
printf("My king, at most 1 road can be built.\n\n");
}else
{
printf("Case %d:\n", Case);
printf("My king, at most %d roads can be built.\n\n", dpIndex);
}
}
}