hdu1025(nlon(n)最长上升子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025

大致思路:设置两个a,b数组,a数组存储数据,b数组存储最长不降序序列。此算法关键在于设计二分查找。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
using namespace std;
int dp[],a[];
int solve(int n)
{
int len=,low,high,mid;
dp[]=a[];
for(int i=;i<=n;i++)
{
low=;high=len;
while(low<=high)
{
mid=(low+high)/;
if(a[i]>dp[mid])low=mid+;
else high=mid-;
}
dp[low]=a[i];
if(low>len)len=low;
}
return len;
}
int main()
{
int n,x,y,cas=;
while(scanf("%d",&n)>)
{
for(int i=;i<=n;i++)
{
scanf("%d%d",&x,&y);
a[x]=y;
}
int ans=solve(n);
printf("Case %d:\n",cas++);
if(ans==)
printf("My king, at most 1 road can be built.\n");
else
printf("My king, at most %d roads can be built.\n",ans);
printf("\n");
}
}
上一篇:android4.0后无法向Servlet发送请求解决办法


下一篇:UVa 1349 - Optimal Bus Route Design(二分图最佳完美匹配)