結果

問題 No.992 最長増加部分列の数え上げ
ユーザー square1001square1001
提出日時 2020-02-14 21:46:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,273 ms / 2,000 ms
コード長 1,785 bytes
コンパイル時間 1,012 ms
コンパイル使用メモリ 87,908 KB
実行使用メモリ 99,584 KB
最終ジャッジ日時 2024-04-16 01:44:55
合計ジャッジ時間 28,746 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
12,800 KB
testcase_01 AC 9 ms
12,928 KB
testcase_02 AC 10 ms
12,928 KB
testcase_03 AC 10 ms
12,928 KB
testcase_04 AC 397 ms
44,160 KB
testcase_05 AC 261 ms
35,328 KB
testcase_06 AC 492 ms
51,712 KB
testcase_07 AC 340 ms
40,448 KB
testcase_08 AC 156 ms
26,624 KB
testcase_09 AC 354 ms
41,344 KB
testcase_10 AC 487 ms
51,840 KB
testcase_11 AC 638 ms
62,720 KB
testcase_12 AC 99 ms
22,016 KB
testcase_13 AC 320 ms
39,296 KB
testcase_14 AC 327 ms
39,552 KB
testcase_15 AC 98 ms
22,272 KB
testcase_16 AC 1,129 ms
90,752 KB
testcase_17 AC 158 ms
26,624 KB
testcase_18 AC 312 ms
38,656 KB
testcase_19 AC 634 ms
61,824 KB
testcase_20 AC 1,273 ms
99,200 KB
testcase_21 AC 1,265 ms
99,200 KB
testcase_22 AC 1,271 ms
99,456 KB
testcase_23 AC 1,273 ms
99,200 KB
testcase_24 AC 1,262 ms
99,328 KB
testcase_25 AC 1,240 ms
99,456 KB
testcase_26 AC 1,245 ms
99,584 KB
testcase_27 AC 1,243 ms
99,328 KB
testcase_28 AC 1,256 ms
99,456 KB
testcase_29 AC 1,250 ms
99,328 KB
testcase_30 AC 415 ms
31,744 KB
testcase_31 AC 414 ms
31,744 KB
testcase_32 AC 412 ms
31,872 KB
testcase_33 AC 415 ms
31,744 KB
testcase_34 AC 410 ms
31,872 KB
testcase_35 AC 567 ms
31,360 KB
testcase_36 AC 572 ms
31,360 KB
testcase_37 AC 567 ms
31,488 KB
testcase_38 AC 570 ms
31,360 KB
testcase_39 AC 570 ms
31,488 KB
testcase_40 AC 443 ms
35,840 KB
testcase_41 AC 448 ms
35,840 KB
testcase_42 AC 448 ms
35,968 KB
testcase_43 AC 448 ms
35,840 KB
testcase_44 AC 446 ms
35,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int mod = 1000000007;
const int inf = 1012345678;
int sz, seg[524295];
void update(int pos, int val) {
	pos += sz;
	seg[pos] = val;
	while (pos > 1) {
		pos >>= 1;
		seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);
	}
}
int query(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l) return -inf;
	if (a <= l && r <= b) return seg[k];
	int lc = query(a, b, k * 2, l, (l + r) >> 1);
	int rc = query(a, b, k * 2 + 1, (l + r) >> 1, r);
	return max(lc, rc);
}
map<int, int> bit[200009];
void add(int f, int x, int val) {
	for (int i = x + 1; i <= sz; i += i & (-i)) {
		bit[f][i] += val;
		if (bit[f][i] >= mod) bit[f][i] -= mod;
	}
}
int sum(int f, int r) {
	int ans = 0;
	for (int i = r; i >= 1; i -= i & (-i)) {
		ans += bit[f][i];
		if (ans >= mod) ans -= mod;
	}
	return ans;
}
int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	vector<int> perm(N);
	for (int i = 0; i < N; ++i) {
		perm[i] = i;
	}
	sort(perm.begin(), perm.end(), [&](int i, int j) { return A[i] != A[j] ? A[i] < A[j] : i > j; });
	sz = 1;
	while (sz < N) sz *= 2;
	vector<int> dp(N);
	for (int i = 0; i < N; ++i) {
		dp[perm[i]] = query(0, perm[i], 1, 0, sz) + 1;
		if (dp[perm[i]] <= 0) dp[perm[i]] = 1;
		update(perm[i], dp[perm[i]]);
	}
	vector<int> ways(N);
	for (int i = 0; i < N; ++i) {
		if (dp[perm[i]] == 1) {
			ways[perm[i]] = 1;
		}
		else {
			ways[perm[i]] = sum(dp[perm[i]] - 1, perm[i]);
		}
		add(dp[perm[i]], perm[i], ways[perm[i]]);
	}
	int len = *max_element(dp.begin(), dp.end());
	int ans = 0;
	for (int i = 0; i < N; ++i) {
		if (dp[i] == len) {
			ans += ways[i];
			if (ans >= mod) ans -= mod;
		}
	}
	cout << ans << endl;
	return 0;
}
0