This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
#define PROBLEM "https://judge.yosupo.jp/problem/longest_increasing_subsequence" #include "../../template.h" #include "../lis.h" void solve() { int n; cin >> n; vector<int> a(n); for(int& x : a) cin >> x; auto res = lis_strict_trace(a); std::cout << res.size() << std::endl; for (int i : res) std::cout << i << ' '; std::cout << std::endl; }
#line 1 "DP/tests/yosupo_lis.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/longest_increasing_subsequence" #line 1 "template.h" #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; } #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; } #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; } #define sqr(x) ((x) * (x)) // For printing pair, container, etc. // Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class Con, class = decltype(begin(declval<Con>()))> typename enable_if<!is_same<Con, string>::value, ostream&>::type operator << (ostream& out, const Con& con) { out << '{'; for (auto beg = con.begin(), it = beg; it != con.end(); it++) { out << (it == beg ? "" : ", ") << *it; } return out << '}'; } template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) { return print_tuple_utils<0, tuple<U...>>(out, t); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long get_rand(long long r) { return uniform_int_distribution<long long> (0, r-1)(rng); } template<typename T> vector<T> read_vector(int n) { vector<T> res(n); for (int& x : res) cin >> x; return res; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; } #line 1 "DP/lis.h" // Source: http://codeforces.com/blog/entry/13225 // Non-strict. int lis_non_strict(const vector<int>& a) { multiset<int> s; for (int x : a) { s.insert(x); auto it = s.upper_bound(x); if (it != s.end()) s.erase(it); } return s.size(); } // Strict. int lis_strict(const vector<int>& a) { multiset<int> s; for (int x : a) { s.insert(x); auto it = s.lower_bound(x); it++; if (it != s.end()) s.erase(it); } return s.size(); } // Return indices of LIS (strict) vector<int> lis_strict_trace(const vector<int>& a) { int n = (int) a.size(); vector<int> b(n+1, 0), f(n, 0); int answer = 0; for (int i = 0; i < n; i++) { f[i] = lower_bound(b.begin() + 1, b.begin()+answer+1, a[i]) - b.begin(); answer = max(answer, f[i]); b[f[i]] = a[i]; } int require = answer; vector<int> T; for (int i = n-1; i >= 0; i--) { if (f[i] == require) { T.push_back(i); require--; } } reverse(T.begin(), T.end()); return T; } // Count number of LIS using mint = long long; // Cnt is exponential. Check if statement says ModInt here? // Returns: (length of LIS, number of LIS) pair<int,mint> count_lis(const vector<int>& a) { if (a.empty()) { return {0, 1}; } // dp[i] = [ (last value, accumulate count) ] for increasing seq of // length i+1 // last value are decreasing vector<vector<pair<int,mint>>> dp(a.size() + 1); int max_len = 0; // returns true if we can append `val` to LIS stored at `cur`. auto pred_len = [] (const vector<pair<int, mint>>& cur, int val) { return !cur.empty() && cur.back().first < val; }; // returns true if we can append `val` after the LIS represented with `p`. auto pred_val = [] (int val, const pair<int,mint>& p) { return val > p.first; }; for (int x : a) { int len = lower_bound(dp.begin(), dp.end(), x, pred_len) - dp.begin(); mint cnt = 1; if (len >= 1) { int pos = upper_bound(dp[len-1].begin(), dp[len-1].end(), x, pred_val) - dp[len-1].begin(); cnt = dp[len-1].back().second; cnt -= (pos == 0) ? 0 : dp[len-1][pos-1].second; } dp[len].emplace_back(x, cnt + (dp[len].empty() ? 0 : dp[len].back().second)); max_len = max(max_len, len + 1); } assert(max_len > 0); return { max_len, dp[max_len-1].back().second, }; } #line 5 "DP/tests/yosupo_lis.test.cpp" void solve() { int n; cin >> n; vector<int> a(n); for(int& x : a) cin >> x; auto res = lis_strict_trace(a); std::cout << res.size() << std::endl; for (int i : res) std::cout << i << ' '; std::cout << std::endl; }