标签:dp
把题意转换一下就是最大递增子序列问题 不过就是数的排列不是按输入顺序#include<stdio.h> #include<string.h> #include<iostream> using namespace std; int dis[500010],num[500010]; int main() { int n,i,j,a,b,d=1; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { scanf("%d%d",&a,&b); num[a]=b; } int len=1; int L,R; dis[1]=num[1]; for(i=2;i<=n;i++) { if(dis[len]<num[i]) { len++; dis[len]=num[i]; } else if(dis[len]>num[i]) { L=1; R=len; int flash=0; while(L<=R) { int mid=(L+R)/2; if(dis[mid]<num[i]) L=mid+1; else if(dis[mid]>num[i])R=mid-1; else {flash==1;break;} } if(!flash)dis[L]=num[i]; } } printf("Case %d:\n",d++); if(len==1) printf("My king, at most 1 road can be built."); else printf("My king, at most %d roads can be built.",len); printf("\n"); printf("\n"); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/zxf654073270/article/details/41450337