ACM. hdu1025

Source: Internet
Author: User

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.

 

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.