标签:
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025
题目:
思路:dp题+二分查找。。。依旧没独立做出来,好惨,最近几个dp题全挂==
题意是找最长上升子序列,
懒得写题解了,不是自己想出来的,上一篇写的比较好的题解:http://blog.csdn.net/dangwenliang/article/details/5728363
这题有两种算法n^2和nlogn的,这题用前一个果断会超时。。。
在二分法上卡半天,我也是服了我的智商。。
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <stack> 8 #include <map> 9 #include <vector> 10 11 #define PI acos((double)-1) 12 #define E exp(double(1)) 13 using namespace std; 14 15 #define N 500009 16 struct point 17 { 18 int x,y; 19 }; 20 21 struct point city[N]; 22 int a[N]; 23 24 bool cmp(point &a,point &b) 25 { 26 return a.x<b.x; 27 } 28 29 int found(int x,int len) 30 { 31 int l,r,mid; 32 l = 0; 33 r = len; 34 mid = (l+r)/2; 35 while(l <= r) 36 { 37 if(a[mid] < x) 38 { 39 l = mid+1; 40 41 } 42 else if(a[mid] > x) 43 { 44 r = mid-1; 45 } 46 mid = (l+r)/2; 47 } 48 return l; 49 } 50 int main (void) 51 { 52 int n,k=0; 53 while(scanf("%d",&n) == 1) 54 { 55 int len; 56 k++; 57 for(int i = 1;i<=n;i++) 58 { 59 scanf("%d%d",&city[i].x,&city[i].y); 60 a[i]= N + 5; 61 } 62 sort(city+1,city+n+1,cmp); 63 a[0] = -1; 64 a[1] = city[1].y; 65 len = 1; 66 for(int i = 2;i<=n;i++) 67 { 68 int j = found(city[i].y,len); 69 a[j] = city[i].y; 70 if(len < j) 71 len = j; 72 } 73 if(len == 1) 74 printf("Case %d:\nMy king, at most %d road can be built.\n\n",k,len); 75 else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",k,len); 76 } 77 return 0; 78 }
杭电1025Constructing Roads In JGShining's Kingdom
标签:
原文地址:http://www.cnblogs.com/weeping/p/5369729.html