链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025
另附:http://acm.nyist.net/JudgeOnline/problem.php?pid=17
源代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define N 500005
int b[N];
int p[N];
int bsearch(int temp,int k);
int LIS();
int n;
int main()
{
int i,cases=1,temp1,temp2;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d %d",&temp1,&temp2);
p[temp1]=temp2;
}
int num=LIS();
printf("Case %d:\nMy king, at most %d road",cases,num);
if(num!=1)printf("s");
printf(" can be built.\n\n");
cases++;
}
return 0;
}
int bsearch(int temp,int k)
{
int low=1;
int high=k;
while(low<=high)
{
int middle=(low+high)/2;
if(temp>=b[middle])low=middle+1;
else high=middle-1;
}
return low;
}
int LIS()
{
int i;
int k=1;
b[1]=p[1];
for(i=2;i<=n;i++)
{
if(p[i]>=b[k])b[++k]=p[i];
else
{
int pos=bsearch(p[i],k);
b[pos]=p[i];
}
}
return k;
}