Ultraviolet A 1471-defense lines (scan + binary)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.