您的位置:首页 > 其它

HDU 1950 Bridging signals (最长递增子序列(nlogn算法)

2017-02-24 17:21 295 查看
题意:题目不怎么好读,好多生僻单词,和图一对比就清楚了,就是端口对接,在连线不交叉的情况下,最多连线条数。

思路:最长递增子序列(nlogn算法),和hdu1025思路一样。

AC代码如下;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=4e4+10;
int p;
int mp[maxn];
int t[maxn];
int len;
int binary_serch1(int i){
int left=1,right=len;
while(left<=right){
int mid=(left+right)/2;
if(mp[i]>t[mid])
left=mid+1;
else right=mid-1;
}
return left;
}
int main(){
int n;
scanf("%d",&n);
while(n--){
scanf("%d",&p);
for(int i=1;i<=p;i++)
scanf("%d",&mp[i]);
t[1]=mp[1];
len=1;
for(int i=2;i<=p;i++){
if(mp[i]>t[len]){
t[++len]=mp[i];
}
else {
int k=binary_serch1(i);
t[k]=mp[i];
}
}
printf("%d\n",len);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: