hdu 1025 上面n个点与下面n个点对应连线 求最多能连有多少条不相交的线 (LIS)

题目大意
有2n个城市,其中有n个富有的城市,n个贫穷的城市,其中富有的城市只在一种资源富有,且富有的城市之间富有的资源都不相同,贫穷的城市只有一种资源贫穷,且各不相同,现在给出一部分贫穷城市的需求,每个需求都是一个贫穷的向一个富有的城市要资源,且每个富有的城市都想向贫穷的城市输入自己富有的那部分资源,现在为了运输要建设多条路,但是路与路之间不允许有交叉,求满足贫穷城市的各种要求最多可以建设多少条路

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

解题思路:
将贫穷的城市按从小到大的顺序排列,然后求富有的城市序号的最大上升子序列LIS解决即可

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.

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 int f[500010] ;
10 //int dp[500010] ;
11 int n ;
12
13 struct city
14 {
15     int p ;
16     int r ;
17 }a[500010];
18
19 bool cmp(const city &x , const city &y)
20 {
21     return x.p < y.p ;
22 }
23
24 int bsearch(int size, const int &a) {
25     int l=0, r=size-1;
26     while( l <= r ){
27         int mid = (l+r)/2;
28         if( a > f[mid-1] && a <= f[mid] ) return mid;// >&&<= 换为: >= && <
29         else if( a < f[mid] ) r = mid-1;
30         else l = mid+1;
31     }
32 }
33
34 int LIS()
35 {
36     int i, j, size = 1;
37     f[0] = a[0].r;
38     //dp[0] = 1;
39     for( i=1; i < n; ++i )
40     {
41         if( a[i].r <= f[0] ) j = 0; // <= 换为: <
42         else if( a[i].r > f[size-1] ) j = size++;// > 换为: >=
43         else j = bsearch(size, a[i].r);
44         f[j] = a[i].r;
45         //dp[i] = j+1;
46     }
47     return size;
48 }
49
50 int main ()
51 {
52     //freopen("in.txt","r",stdin) ;
53     int Case = 1 ;
54     while(scanf("%d" , &n) !=EOF)
55     {
56         printf("Case %d:\n" , Case) ;
57         Case++ ;
58         int i ;
59         for (i = 0 ; i < n ; i++)
60         {
61             scanf("%d %d" , &a[i].p , &a[i].r) ;
62         }
63         sort(a,a+n,cmp) ;
64         int ans = LIS() ;
65         if (ans == 1)
66             printf("My king, at most %d road can be built.\n" , ans) ;
67         else
68             printf("My king, at most %d roads can be built.\n" , ans) ;
69         printf("\n") ;
70
71     }
72
73
74     return 0 ;
75 }

时间: 2024-08-08 22:34:02

hdu 1025 上面n个点与下面n个点对应连线 求最多能连有多少条不相交的线 (LIS)的相关文章

Constructing Roads In JGShining&#39;s Kingdom HDU - 1025

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)

Constructing Roads In JGShining&#39;s Kingdom(HDU 1025 LIS nlogn方法)

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21002    Accepted Submission(s): 5935 Problem Description JGShining's kingdom consists of 2n(n is no mor

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom   LIS 简单题 好题 超级坑

Constructing Roads In JGShining's Kingdom 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) whi

HDU 1025:Constructing Roads In JGShining&#39;s Kingdom(LIS+二分优化)

http://acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom 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 r

!HDU 1025 Constructing Roads In JGShining&#39;s Kingdom--DP--(LIS算法)

题意:在马路两边分别有n个城市,给出期望的n条路用于连接两边的城市,但是要求路不能有交叉,求在期望的n条中路实际能保留下来的最大的条数 分析:这题很好 1.本题抽象出来的模型应该是求最长上升(不下降)子序列 2.LIS的 nlog(n)算法: O(n^2) 的算法是dp[i]保留以i结尾的最长上升子序列的长度,令k=dp[i],O(nlog(n))算法是从k的角度出发,设d(k)为在长度为 k 的序列中的最小的位置,即:d(k)=min(a[i]),其中 f[i]=k,然后二分,每次看a[i]是

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom (DP)

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 ca

hdu 1025 Constructing Roads In JGShining&#39;s Kingdom(二分法+最长上升子序列)

题目大意:河的两岸有两个不同的国家,一边是穷国,一边是富国,穷国和富国的村庄的标号是固定的,穷国要变富需要和富国进行交流,需要建桥,并且建的桥不能够有交叉.问最多可以建多少座桥.      思路:建路时如下图所示                 当一边的点已经固定了的时候,另外一边按照从小到大的序列与当前的边连接,得到最少的交叉.           题目给的第二组测试数据,如果按照图一则可以建2座桥,图二建一座桥 3 1 2 2 3 3 1                           

Hdu 1025(LIS)

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15126    Accepted Submission(s): 4300 Problem Description JGShining's kingdom consists of 2n(n is no mor

HDU 1025 Constructing Roads In JGShining&amp;#39;s Kingdom (DP)

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 ca