您的位置:首页 > 其它

最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom

2017-08-02 15:46 459 查看
题目地址:HDU 1025

用简单的DP复杂度为n*n 会TLE 

找了一个用另外一种方法做的 复杂度为nlogn

最长上升子序列

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define N 500050
using namespace std;

int road
,n;
int dp
,m;
int find(int t) {
int l=1,r=m,mid;
while(l<=r) {
mid=(l+r)/2;
if(dp[mid]>t)
r=mid-1;
else
l=mid+1;
}
return l;
}
int main() {
int x,y,p=0;
while(~scanf("%d",&n)) {
for(int i=1; i<=n; i++) {
scanf("%d %d",&x,&y);
road[x]=y;
}
m=1;
dp[1]=road[1];
for(int i=2; i<=n; i++) {
if(dp[m]<road[i])
dp[++m]=road[i];
else {
int t=find(road[i]);
dp[t]=road[i];
}
}
if(m==1)
printf("Case %d:\nMy king, at most %d road can be built.\n\n",++p,m);
else
printf("Case %d:\nMy king, at most %d roads can be built.\n\n",++p,m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐