結果
問題 | No.992 最長増加部分列の数え上げ |
ユーザー |
![]() |
提出日時 | 2020-05-05 16:41:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,602 bytes |
コンパイル時間 | 3,123 ms |
コンパイル使用メモリ | 208,028 KB |
最終ジャッジ日時 | 2025-01-10 06:51:15 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 TLE * 11 |
ソースコード
#include <bits/stdc++.h>using namespace std;inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}typedef long long ll;typedef vector<int> VI;typedef vector<VI> VVI;typedef vector<long long> VL;typedef vector<vector<long long>> VVL;typedef vector<string> VS;typedef pair<int, int> P;typedef tuple<int,int,int> tpl;#define ALL(a) (a).begin(),(a).end()#define SORT(c) sort((c).begin(),(c).end())#define REVERSE(c) reverse((c).begin(),(c).end())#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()#define FOR(i,a,b) for(int i=(a);i<(b);++i)#define REP(i,n) FOR(i,0,n)#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)#define RREP(i,n) RFOR(i,n,0)#define en "\n"constexpr double EPS = 1e-9;constexpr double PI = 3.141592653589793238462643383279;constexpr int INF = 2147483647;constexpr long long LINF = 1LL<<60;constexpr long long MOD = 1000000007; // 998244353#define dump(x) cerr << #x << " = " << (x) << endl;#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }template<typename T>struct BIT{using Func = function<T(T, T)>;int n;vector<T> node;T init_v;Func func;public:BIT(int N, Func _func, T _init_v) {n = N + 1;init_v = _init_v;func = _func;node.resize(n, init_v);for(int i=1; i<n; i++) if((i+(i&-i)) < n) node[i+(i&-i)] = func(node[i+(i&-i)], node[i]);}BIT() {}void add(int pos, T v) {// 1-indexedif(pos == 0) return;while(pos < n){node[pos] = func(node[pos], v);pos += pos & -pos;}}T sum(int pos){// 1-indexedT res = init_v;while(pos > 0){res = func(res, node[pos]);pos = pos & (pos-1);}return res;}T get(int pos){// 1-indexedreturn node[pos];}};void compress(vector<int> &v){int N = v.size(), mn = 1000000000;vector<pair<int,int>> a(N);for(int i=0; i<N; ++i){a[i].first = v[i];a[i].second = i;mn = min(mn, v[i]);}sort(a.begin(), a.end());int prev = mn-1, n = -1;for(int i=0; i<N; ++i){if(a[i].first == prev) v[a[i].second] = n;else{n++;prev = a[i].first;v[a[i].second] = n;}}}int main(void){int N; cin >> N;VI A(N); REP(i,N) cin >> A[i];compress(A);VI dp, len(N), mx(N+1,0);int maxlen = 0;REP(i,N){int t = LB(dp, A[i]);if(t < dp.size()) chmin(dp[t], A[i]);else dp.push_back(A[i]);len[i] = t+1;chmax(mx[len[i]], A[i]+1);chmax(maxlen, len[i]);}vector<BIT<int>> v(N+1);REP(i,N+1) v[i] = BIT<int>(mx[i], [](int a, int b){return (a+b)%MOD;}, 0);REP(i,N){int idx = min(v[len[i]-1].n-1, A[i]);ll n = len[i]>1 ? v[len[i]-1].sum(idx) : 1;v[len[i]].add(A[i]+1, n);}ll ans = v[maxlen].sum(mx[maxlen]);cout << ans << en;return 0;}