Ultraviolet A 1471-defense lines
Question Link
For a given sequence, it is required to delete a continuous subsequence. The obtained sequence has the longest continuous increasing sequence and the maximum length of the continuous increasing sequence is output.
Train of Thought: first scan left and right, record the maximum length of each position to the left and to the right, and then scan again from left to right, open an array min to record the sequence of length I, the minimum value of the last bit. This sequence satisfies monotonicity, because the incremental sequence must be 1, 2, 3, 4... in this way, if a A [I] is small, the value of the corresponding length of Min is maintained. In this way, in the monotonically increasing sequence, each time, we can find the last digit less than a [I] in two parts, and then splice it with the sequence length from the current position to the right to get a length. The maximum value recorded is the answer.
Code:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int N = 200005;int t, n, a[N], left[N], right[N], Min[N];void init() { scanf("%d", &n); left[0] = 1; right[n - 1] = 1; for (int i = 0; i < n; i++) {scanf("%d", &a[i]);if (i) { left[i] = 1; if (a[i] > a[i - 1]) left[i] += left[i - 1];} } for (int i = n - 2; i >= 0; i--) {right[i] = 1;if (a[i] < a[i + 1]) right[i] += right[i + 1]; }}int solve() { int ans = 0; memset(Min, INF, sizeof(Min)); for (int i = 0; i < n; i++) {int len = lower_bound(Min + 1, Min + 1 + n, a[i]) - Min;ans = max(ans, right[i] + len - 1);Min[left[i]] = min(Min[left[i]], a[i]); } return ans;}int main() { scanf("%d", &t); while (t--) {init();printf("%d\n", solve()); } return 0;}
Ultraviolet A 1471-defense lines (scan + binary)