結果

問題 No.992 最長増加部分列の数え上げ
ユーザー leaf_1415leaf_1415
提出日時 2020-02-14 22:34:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 2,609 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 76,388 KB
実行使用メモリ 28,836 KB
最終ジャッジ日時 2024-04-16 02:41:41
合計ジャッジ時間 7,501 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
16,768 KB
testcase_01 AC 14 ms
16,640 KB
testcase_02 AC 14 ms
16,640 KB
testcase_03 AC 14 ms
16,640 KB
testcase_04 AC 70 ms
20,972 KB
testcase_05 AC 54 ms
19,916 KB
testcase_06 AC 83 ms
21,624 KB
testcase_07 AC 64 ms
20,392 KB
testcase_08 AC 39 ms
18,732 KB
testcase_09 AC 65 ms
20,664 KB
testcase_10 AC 83 ms
21,624 KB
testcase_11 AC 103 ms
22,968 KB
testcase_12 AC 30 ms
17,920 KB
testcase_13 AC 61 ms
20,372 KB
testcase_14 AC 62 ms
20,376 KB
testcase_15 AC 30 ms
17,920 KB
testcase_16 AC 160 ms
27,024 KB
testcase_17 AC 39 ms
18,732 KB
testcase_18 AC 62 ms
20,240 KB
testcase_19 AC 103 ms
22,948 KB
testcase_20 AC 178 ms
27,936 KB
testcase_21 AC 179 ms
27,932 KB
testcase_22 AC 182 ms
28,064 KB
testcase_23 AC 176 ms
27,808 KB
testcase_24 AC 176 ms
27,932 KB
testcase_25 AC 180 ms
27,812 KB
testcase_26 AC 176 ms
27,936 KB
testcase_27 AC 179 ms
27,808 KB
testcase_28 AC 179 ms
27,844 KB
testcase_29 AC 181 ms
27,808 KB
testcase_30 AC 119 ms
28,060 KB
testcase_31 AC 117 ms
27,936 KB
testcase_32 AC 120 ms
27,932 KB
testcase_33 AC 118 ms
27,936 KB
testcase_34 AC 119 ms
27,680 KB
testcase_35 AC 106 ms
27,552 KB
testcase_36 AC 106 ms
27,552 KB
testcase_37 AC 107 ms
27,596 KB
testcase_38 AC 107 ms
27,548 KB
testcase_39 AC 107 ms
27,552 KB
testcase_40 AC 128 ms
28,320 KB
testcase_41 AC 129 ms
28,320 KB
testcase_42 AC 133 ms
28,836 KB
testcase_43 AC 130 ms
28,324 KB
testcase_44 AC 128 ms
28,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define llint long long
#define inf 1e18
#define mod 1000000007

using namespace std;

struct SegTree{
	int size;
	vector<llint> seg;

	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}

	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = -inf;
	}

	void update(int i, llint val)
	{
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = max(seg[i*2], seg[i*2+1]);
		}
	}

	llint query(int a, int b, int k, int l, int r)
	{
		if(b < l || r < a) return -inf;
		if(a <= l && r <= b) return seg[k];
		llint lval = query(a, b, k*2, l, (l+r)/2);
		llint rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return max(lval, rval);
	}
	llint query(int a, int b)
	{
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

llint n;
llint a[200005];
vector<llint> comp;
SegTree seg(18);
llint dp[200005], dp2[200005];
vector<llint> vec[200005], svec[200005];

int main(void)
{
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for(int i = 1; i <= n; i++) cin >> a[i];
  for(int i = 1; i <= n; i++) comp.push_back(a[i]);
  comp.push_back(-inf);
  sort(comp.begin(), comp.end());
  comp.erase(unique(comp.begin(), comp.end()), comp.end());
  for(int i = 1; i <= n; i++) a[i] = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();

  seg.init();
  seg.update(0, 0);
  dp[0] = 0;
  for(int i = 1; i <= n; i++){
    dp[i] = seg.query(0, a[i]-1) + 1;
    seg.update(a[i], dp[i]);
    //for(int j = 0; j < 10; j++) cout << seg.query(j, j) << " "; cout << endl;
  }
  llint lis = seg.query(0, (int)comp.size());

  //for(int i = 0; i <= n; i++) cout << dp[i] << " "; cout << endl;

  for(int i = 0; i <= n; i++) vec[dp[i]].push_back(i);
  svec[0].push_back(1);
  dp2[0] = 1;

  for(int i = 1; i <= n; i++){
    vector<llint> &cvec = vec[dp[i]-1];
    llint p = lower_bound(cvec.begin(), cvec.end(), i) - cvec.begin();

    //cout << p << endl;

    llint ub = p, lb = -1, mid;
    while(ub-lb>1){
      mid = (ub+lb)/2;
      if(a[cvec[mid]] < a[i]) ub = mid;
      else lb = mid;
    }
    //cout << ub << endl;
    vector<llint> &csvec = svec[dp[i]-1];
    dp2[i] = csvec[p-1];
    if(ub > 0) dp2[i] += mod - csvec[ub-1] + mod, dp2[i] %= mod;
    if(svec[dp[i]].size() == 0) svec[dp[i]].push_back(dp2[i]);
    else svec[dp[i]].push_back((svec[dp[i]].back() + dp2[i]) % mod);
  }

  //for(int i = 0; i <= n; i++) cout << dp2[i] << " "; cout << endl;

  llint ans = 0;
  for(int i = 0; i <= n; i++){
    if(dp[i] == lis) ans += dp2[i], ans %= mod;
  }
  cout << ans << endl;

  return 0;
}
0