题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025
2 1 2 2 1 3 1 2 2 3 3 1
Case 1: My king, at most 1 road can be built. Case 2: My king, at most 2 roads can be built.HintHuge input, scanf is recommended.
(2)回到这题,我们用一个结构体存Line I中的n个数 和 Line II中的n个数 ,然后按照Line I 中的n个数从小到大将结构体排序,那么此时在Line II中去找最长上升子序列即可。
( 3 )最后说一下 这题中的一个坑,输出road 和roads 是有区别的
PS:先做POJ3903,再做这题~~~~
附上代码;
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <cstdio> #include <algorithm> #include <cmath> const int maxn=500500; const int INF=99999999; using namespace std; struct node { int s,e; }a[maxn]; int d[maxn]; int cmp(node a,node b) { return a.s<b.s; } int find(int l,int r,int key) { if(l==r)return l; int mid=(l+r)/2; if(key>d[mid])return find(mid+1,r,key); else return find(l,mid,key); } int main() { int n,test=1; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%d%d",&a[i].s,&a[i].e); sort(a,a+n,cmp); int len=0; d[0]=-INF; for(int i=0;i<n;i++) { if(a[i].e>d[len])d[++len]=a[i].e; else { int j=find(1,len,a[i].e); d[j]=a[i].e; } } if(len==1||len==0) printf("Case %d:\nMy king, at most %d road can be built.\n\n",test++,len); else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",test++,len); } return 0; }
原文地址:http://blog.csdn.net/liusuangeng/article/details/38899891