您的位置:首页 > 其它

HDU-1257 杭电 最少拦截系统——二分优化的DP算法(需回头理解)

2018-01-25 15:29 537 查看
#include "iostream"
#include "vector"
#include "algorithm"

using namespace std;
int comp(const int &a,const int &b){
return a < b;
}
int main(){
int n,m;
int i, j;
int maxscore;
vector<int> a;
vector<int> system;
int count = 1;
vector<int>::iterator location;
while(scanf("%d",&n) != EOF){
for ( i = 0; i < n; i++){
scanf("%d",&m);
a.push_back(m);
}
system.push_back(a[0]);

for (vector<int>::iterator it = a.begin(); it != a.end(); it++){
sort(system.begin(),system.end(),comp);
location = lower_bound(system.begin(),system.end(),*it);//lower_bound()在first和last中的前闭后开区间进行二分查找
if(location == system.end()){
count++;
system.push_back(*it);
}
else *location = *it;
}

printf("%d\n",count);
a.clear();
system.clear();
count = 1;
}
return 0;
}


本题我使用了两个vector进行排列,a为存储原始数据,system代表子序列,只要每次看是否在子序列中,如果在,就更新子序列,并排序,如果不在,就加入子序列中,代表新的系统。

一个序列中,最长上升子序列的长度就是不下降子序列的个数。

最长上升子序列nlogn及n^2算法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: