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.
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[500005]; struct node { int x,y; }s[500005]; bool cmp(node a,node b) { return a.x<b.x; } int main() { int ant=0,n,i,j,c; while(~scanf("%d",&n)) { ant++; for(i=0;i<n;i++) scanf("%d%d",&s[i].x,&s[i].y); sort(s,s+n,cmp); c=0;dp[c]=s[0].y;//二分开始。 for(i=1;i<n;i++) { if(dp[c]<s[i].y) { dp[++c]=s[i].y; } else { int l,r,mid; l=0;r=c; while(l<r) { mid=(l+r)/2; if(dp[mid]<s[i].y) l=mid+1; else r=mid; } mid=r; dp[mid]=s[i].y; } } if(c==0)printf("Case %d:\nMy king, at most %d road can be built.\n\n",ant,c+1); else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",ant,c+1); } return 0; }
杭电 1025 Constructing Roads In JGShining's Kingdom(二分查找),布布扣,bubuko.com
杭电 1025 Constructing Roads In JGShining's Kingdom(二分查找)
原文地址:http://blog.csdn.net/u012766950/article/details/38386723