hdu1257 最少拦截系统

http://acm.hdu.edu.cn/showproblem.php?pid=1257

#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
const int maxn=1e6+10;
int data[maxn];
int res[maxn];//len的元素个数即为序列长度
int n,len=1;
int main(){
  while(cin >> n){//测试数据数量未知
    memset(data,0,sizeof(data));
    memset(res,0,sizeof(res));

    for(int i = 1 ; i <= n ; i++){
      cin >> data[i];
    }
    res[1]=data[1];
    for(int i = 2 ; i <= n ; i++){
      if(data[i] > res[len]){//不可放入已存的拦截系统
        res[++len]=data[i];//res长度加一,存入data[i]
      }else{
        int temp=lower_bound(res+1,res+len+1,data[i])-res;
        //返回[res+1,res+len+1)中第一个大于等于data[i]的元素下标,若没有,则返回res+len+1

        res[temp]=data[i];//直接用data[i]覆盖
      }
    }
    cout << len << endl;//len即为拦截系统个数

  }
  return 0;
}

标签:hdu/二分/贪心/LIS 

原文地址:https://www.cnblogs.com/ecustlegendn324/p/12013279.html