最长上升子序列(N*log(N))hdu1025

(HDU1025)

Constructing Roads In JGShining's Kingdom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18804    Accepted Submission(s): 5311


Problem Description
JGShining'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 should 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 economic 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 many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^
 
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. Case 2: My king, at most 2 roads can be built.

 题意:上面n个点,下面n个点,然后在这2n个点之间随意连线,一个点只能被连一次,问最多有多少条线不交叉。

方法一:upper_bound()容器

 1 #include"stdio.h"
 2 #include"string.h"
 3 #include"stdlib.h"
 4 #include"algorithm"
 5 #include"queue"
 6 #include"math.h"
 7 #include"iostream"
 8 #include"vector"
 9 #define M 100009
10 #define inf 0x3f3f3f3f
11 #define eps 1e-9
12 #define PI acos(-1.0)
13 #include"map"
14 #include"vector"
15 #include"set"
16 #include"string"
17 #include"stack"
18 #define LL __int64
19 using namespace std;
20 int a[M],b[M],n;
21 int finde()
22 {
23     int t=0;
24     b[t]=a[1];
25     t++;
26     for(int i=2;i<=n;i++)
27     {
28         int id=upper_bound(b,b+t,a[i])-b;
29         //在b数组中弹出比ai大的最左边的元素,然后返回下标,否则返回last的下标
30         if(id==t)
31             t++;
32         b[id]=a[i];
33     }
34     return t;
35 }
36 int main()
37 {
38     int kk=1;
39     while(scanf("%d",&n)!=-1)
40     {
41 
42         for(int i=1;i<=n;i++)
43         {
44             int k,p;
45             scanf("%d%d",&k,&p);
46             a[k]=p;
47         }
48         int leng=finde();
49         printf("Case %d:
",kk++);
50         if(leng==1)
51             cout<<"My king, at most "<< leng <<" road can be built."<<endl;
52         else
53             cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
54         cout<<endl;
55     }
56     return 0;
57 }
View Code

方法二:二分查找

 1 #include"stdio.h"
 2 #include"string.h"
 3 #include"stdlib.h"
 4 #include"algorithm"
 5 #include"queue"
 6 #include"math.h"
 7 #include"iostream"
 8 #include"vector"
 9 #define M 100009
10 #define inf 0x3f3f3f3f
11 #define eps 1e-9
12 #define PI acos(-1.0)
13 #include"map"
14 #include"vector"
15 #include"set"
16 #include"string"
17 #include"stack"
18 #define LL __int64
19 using namespace std;
20 int a[M],b[M],c[M],n;
21 int finde(int n,int k)
22 {
23     int l=1;
24     int r=n;
25     while(l<=r)
26     {
27         int mid=(l+r)/2;
28         if(c[mid]<k)
29             l=mid+1;
30         else
31             r=mid-1;
32     }
33     return l;
34 }
35 int main()
36 {
37     int kk=1;
38     while(scanf("%d",&n)!=-1)
39     {
40 
41         for(int i=1;i<=n;i++)
42         {
43             int k,p;
44             scanf("%d%d",&k,&p);
45             a[k]=p;
46         }
47         memset(c,inf,sizeof(c));
48         b[1]=1;
49         c[1]=a[1];
50         for(int i=2;i<=n;i++)
51         {
52             int id=finde(n,a[i]);
53             c[id]=a[i];
54             b[i]=id;
55         }
56         int leng=0;
57         for(int i=1;i<=n;i++)
58             leng=max(leng,b[i]);
59 
60         printf("Case %d:
",kk++);
61         if(leng==1)
62             cout<<"My king, at most "<< leng <<" road can be built."<<endl;
63         else
64             cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
65         cout<<endl;
66     }
67     return 0;
68 }
View Code

 方法三:模拟upper_bound

 1 #include"stdio.h"
 2 #include"string.h"
 3 #include"stdlib.h"
 4 #include"algorithm"
 5 #include"queue"
 6 #include"math.h"
 7 #include"iostream"
 8 #include"vector"
 9 #define M 100009
10 #define inf 0x3f3f3f3f
11 #define eps 1e-9
12 #define PI acos(-1.0)
13 #include"map"
14 #include"vector"
15 #include"set"
16 #include"string"
17 #include"stack"
18 #define LL __int64
19 using namespace std;
20 int a[M],b[M],c[M],n;
21 int binary_find(int n,int k)
22 {
23     int l=0;
24     int r=n-1;
25     while(l<=r)
26     {
27         int mid=(l+r)/2;
28         if(b[mid]>=k)
29             r=mid-1;
30         else
31             l=mid+1;
32     }
33     return l;
34 }
35 int fun(int n)
36 {
37     int t=0;
38     b[t]=a[1];
39     t++;
40     for(int i=2;i<=n;i++)
41     {
42         int id=binary_find(t,a[i]);
43         if(id==t)
44             t++;
45         b[id]=a[i];
46     }
47     return t;
48 }
49 int main()
50 {
51     int kk=1;
52     while(scanf("%d",&n)!=-1)
53     {
54 
55         for(int i=1;i<=n;i++)
56         {
57             int k,p;
58             scanf("%d%d",&k,&p);
59             a[k]=p;
60         }
61         int leng=fun(n);
62         printf("Case %d:
",kk++);
63         if(leng==1)
64             cout<<"My king, at most "<< leng <<" road can be built."<<endl;
65         else
66             cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
67         cout<<endl;
68     }
69     return 0;
70 }
View Code
原文地址:https://www.cnblogs.com/mypsq/p/4710529.html