首页 > 代码库 > ACM.hdu1025
ACM.hdu1025
to get the ans of how many roads at most that can be built between two line without intersection of roads,we need sort the input sequence at ont edge and then deal with anther line with LIS
way,meanwhile, not forget using the nlogn way.
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const int N=500009; 5 struct LINE 6 { 7 int from,to; 8 friend bool operator<(LINE A,LINE B) 9 {10 return A.from<B.from;11 }12 };13 LINE node[N];14 int y[N];15 int c[N];16 int MID(int a[],int value,int size)17 {18 int l=1,r=size;19 int mid=(l+r)/2;20 while(l<r)//1 3 5 7 9 321 {22 if(a[mid]<value&&value<=a[mid+1])return mid+1;23 else if(value<=a[mid])r=mid;24 else l=mid+1;25 mid=(l+r)/2;26 }27 }28 int LIS(int len)29 {30 int size=1;31 int zhong=0;32 c[1]=y[1];33 for(int i=2;i<=len;i++)//7 6 4 9 11 14 17 3 15 20 4 5 6 7 8 34 {//3 9 11 15 17 2035 if(y[i]<=c[1])c[1]=y[i];36 else if(y[i]>c[size])37 c[++size]=y[i];38 else 39 {40 zhong=MID(c,y[i],size);41 c[zhong]=y[i];42 }43 }44 return size;45 }46 int main()47 {48 int n;49 int cnt=1;50 while(cin>>n)51 {52 for(int i=0;i<n;i++)53 scanf("%d %d",&node[i].from,&node[i].to);54 sort(node,node+n);//sort one side by ascending direction55 for(int i=0;i<n;i++)56 y[i+1]=node[i].to;57 int ans=LIS(n);58 cout<<"Case "<<cnt<<‘:‘<<endl;59 cout<<"My king, at most "<<ans;60 if(ans>1)cout<<" roads can be built."<<endl;61 else cout<<" road can be built."<<endl;62 cnt++;63 cout<<endl;64 }65 return 0;66 }
It has used the longest increasing subsequence algorithm way,and to get it solved after you have learned it.
input : with a n,represent n pairs of integers followed.And n pairs of intergers followed
output:
Cast i:
My king, at most n road(s) can be built
A sample below:
11
2 5
3 6
4 2
5 1
6 9
7 11
8 7
9 12
10 8
11 4
12 10
Case 1:
My king, at most 6 road can be built.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。