求最长上升子序列的二分法。
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define MAX(a,b) (a>b)?a:b const int SIZE=500000+16; const int INF=1000000; struct node{ int rich; int poor; }; node road[SIZE]; int dp[SIZE]; bool cmp(node no1,node no2) { return no1.poor < no2.poor; } int main() { int n; int t=0; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf("%d %d",&road[i].poor,&road[i].rich); } memset(dp,INF,sizeof(int)*n); sort(road,road+n,cmp); int ans=-1; for(int i=0;i<n;i++) *upper_bound(dp,dp+n,road[i].rich)=road[i].rich; ans=upper_bound(dp,dp+n,INF)-dp; printf("Case %d:\n",++t); printf("My king, at most %d %s can be built.\n",ans,(ans>1)?"roads":"road"); printf("\n"); } return 0; }
时间: 2024-10-23 17:27:44