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

#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算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值