結果

問題 No.992 最長増加部分列の数え上げ
ユーザー square1001square1001
提出日時 2020-02-14 21:47:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,205 ms / 2,000 ms
コード長 1,805 bytes
コンパイル時間 937 ms
コンパイル使用メモリ 90,020 KB
実行使用メモリ 96,640 KB
最終ジャッジ日時 2024-04-16 01:45:57
合計ジャッジ時間 23,893 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
14,464 KB
testcase_01 AC 11 ms
14,592 KB
testcase_02 AC 11 ms
14,464 KB
testcase_03 AC 11 ms
14,592 KB
testcase_04 AC 335 ms
43,264 KB
testcase_05 AC 217 ms
35,456 KB
testcase_06 AC 416 ms
50,048 KB
testcase_07 AC 302 ms
40,192 KB
testcase_08 AC 118 ms
26,624 KB
testcase_09 AC 312 ms
40,832 KB
testcase_10 AC 427 ms
49,792 KB
testcase_11 AC 562 ms
58,752 KB
testcase_12 AC 75 ms
22,784 KB
testcase_13 AC 269 ms
39,552 KB
testcase_14 AC 279 ms
39,424 KB
testcase_15 AC 75 ms
22,912 KB
testcase_16 AC 1,003 ms
88,320 KB
testcase_17 AC 124 ms
26,880 KB
testcase_18 AC 276 ms
38,528 KB
testcase_19 AC 565 ms
57,984 KB
testcase_20 AC 1,179 ms
96,512 KB
testcase_21 AC 1,151 ms
96,128 KB
testcase_22 AC 1,144 ms
96,256 KB
testcase_23 AC 1,146 ms
95,872 KB
testcase_24 AC 1,158 ms
96,000 KB
testcase_25 AC 1,205 ms
96,000 KB
testcase_26 AC 1,146 ms
96,640 KB
testcase_27 AC 1,134 ms
95,872 KB
testcase_28 AC 1,160 ms
96,128 KB
testcase_29 AC 1,165 ms
96,384 KB
testcase_30 AC 269 ms
31,152 KB
testcase_31 AC 265 ms
31,408 KB
testcase_32 AC 269 ms
31,280 KB
testcase_33 AC 266 ms
31,280 KB
testcase_34 AC 264 ms
31,152 KB
testcase_35 AC 270 ms
32,336 KB
testcase_36 AC 274 ms
32,336 KB
testcase_37 AC 273 ms
32,464 KB
testcase_38 AC 270 ms
32,340 KB
testcase_39 AC 273 ms
32,344 KB
testcase_40 AC 293 ms
35,248 KB
testcase_41 AC 296 ms
35,376 KB
testcase_42 AC 297 ms
35,244 KB
testcase_43 AC 299 ms
35,504 KB
testcase_44 AC 300 ms
35,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
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);
}
unordered_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