最长上升子序列 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;
}
用简单的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;
}
相关文章推荐
- HDU 1025-Constructing Roads In JGShining's Kingdom(最长上升子序列 NlogN复杂度)
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- hdu 1025 Constructing Roads In JGShining's Kingdom LIS最长上升序列
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列的长度)
- 杭电ACM OJ 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列 O(nlogn)算法详解+回溯暴力解决+动态规划dp优雅解决
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升序列
- HDU - 1025 Constructing Roads In JGShining's Kingdom (nlogn 最长上升子序列)
- HDU 1025 A - Constructing Roads In JGShining's Kingdom(最长上升子序列)
- 暑期dp46道(16)hdoj 1025 Constructing Roads In JGShining's Kingdom最长上升子序列(二分)
- hdu 1025 Constructing Roads In JGShining's Kingdom--最长上升子串(时间优化)
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升序列nlogn
- HDU 1025(Constructing Roads In JGShining's Kingdom)最长不降子序列nlogn
- 【DP】HDU 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP,LIS最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom (最长上升子序列)
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- HDU - 1025 Constructing Roads In JGShining's Kingdom(最长单调子序列 nlogn)
- HDU 1025 Constructing Roads In JGShining's Kingdom (简单dp, 最长上升子序列LIS)
- HDU 1025 - Constructing Roads In JGShining's Kingdom(最长上升子序列)