cpp_lib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub idat50me/cpp_lib

:heavy_check_mark: LIS(最長増加部分列) (dp/lis.cpp)

なにこれ

最長増加部分列(LIS)の長さを求める.

関数

計算量

v の要素数を $n$ とする.

Verified with

Code

#pragma once

#ifndef call_include
#define call_include
#include <bits/stdc++.h>
using namespace std;
#endif

template<typename T> int LIS(const vector<T> &v, bool strict = true) {
	vector<T> lis;
	if(strict) {
		for(T a : v) {
			auto itr = lower_bound(lis.begin(), lis.end(), a);
			if(itr == lis.end()) lis.emplace_back(a);
			else
				*itr = a;
		}
	}
	else {
		for(T a : v) {
			auto itr = upper_bound(lis.begin(), lis.end(), a);
			if(itr == lis.end()) lis.emplace_back(a);
			else
				*itr = a;
		}
	}
	return lis.size();
}
#line 2 "dp/lis.cpp"

#ifndef call_include
#define call_include
#include <bits/stdc++.h>
using namespace std;
#endif

template<typename T> int LIS(const vector<T> &v, bool strict = true) {
	vector<T> lis;
	if(strict) {
		for(T a : v) {
			auto itr = lower_bound(lis.begin(), lis.end(), a);
			if(itr == lis.end()) lis.emplace_back(a);
			else
				*itr = a;
		}
	}
	else {
		for(T a : v) {
			auto itr = upper_bound(lis.begin(), lis.end(), a);
			if(itr == lis.end()) lis.emplace_back(a);
			else
				*itr = a;
		}
	}
	return lis.size();
}
Back to top page