Namomo Camp Div1 连续子序列

题目链接

  因为找出来的上升子序列是连续的,所以可以考虑用\(dp[x] = dp[x - 1] + 1\)来转移状态,同时要时刻更新是从哪一个数开始的

int n;
std::cin >> n;
std::map<int,int> dp;
std::vector<int> a(n);

int start = 0, mx = 0;

for (int i = 0; i < n; i ++ ) std::cin >> a[i];
for (int i = 0; i < n; i ++ ) {
	dp[a[i]] = dp[a[i] - 1] + 1;
	if (dp[a[i]] > mx || (mx == dp[a[i]] && a[i] - i + 1 < start)) {
		mx = dp[a[i]];
		start = a[i] - mx + 1;
	}
}

std::cout << mx << "\n";
for (int i = 0; i < mx; i ++ ) std::cout << start + i << " \n"[i == mx - 1];
posted @ 2022-07-06 16:57  浅渊  阅读(24)  评论(0)    收藏  举报