标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17424 Accepted Submission(s):
4946
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; const int maxn=1000010; int n; struct city { int a,b; friend bool operator<(city a,city b) { return a.b<b.b; } };city a[maxn]; int d[maxn]; int BinSearch(int *a,int left,int right,int key) { while(left<right){ int mid=(left+right)/2; if(a[mid]<key&&a[mid+1]>=key) return mid; if(a[mid]>=key) right=mid; else left=mid+1; } return 0; } int lis(city *a,int n,int *d) { int len=1; d[1]=a[0].a; for(int i=1;i<n;i++){ if(a[i].a>d[len]) d[++len]=a[i].a; else{ int k=BinSearch(d,1,len,a[i].a);///二分查找求第一个比a[i]小的数的下标 d[k+1]=a[i].a; } } return len; } int main() { int tag=1; while(cin>>n){ for(int i=0;i<n;i++){ scanf("%d%d",&(a[i].a),&(a[i].b)); } sort(a,a+n); int ans=lis(a,n,d); printf("Case %d:\n",tag++); if(ans<=1) printf("My king, at most %d road can be built.\n\n",ans); else printf("My king, at most %d roads can be built.\n\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4354858.html