Problem Description
Problem Analysis
There are some edge cases that are not easy to think of.
The more obvious idea is to traverse the array and compute the maximum length you could get if no modification were allowed.
If modifying some number yields the maximum length, that means modifying this number can join the length on the left with the length on the right.
Following this line of thought, you can then reason about the edge cases and determine whether the two parts can be joined.
To obtain the lengths in O(N), you can compute the maximum length ending at i and the maximum length starting at i separately (a reverse traversal suffices), which makes it convenient to focus, while traversing, on whether i can splice the left and right parts together through a modification.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| #include <iostream>
int a[100020];
int endAns[100020]; // 以i结尾的最长递增子序列
int startAns[100020]; // 以i开头的最长递增子序列
int main()
{
int T;
std::cin >> T;
while (T--)
{
int n;
std::cin >> n;
for (int i = 0; i < n; ++i)
{
std::cin >> a[i];
}
int tempAns = 0;
for (int i = 0; i < n; ++i)
{
if (i == 0 || a[i] > a[i - 1])
{
++tempAns;
}
else
{
tempAns = 1;
}
endAns[i] = tempAns;
}
tempAns = 0;
for (int i = n - 1; i >= 0; --i)
{
if (i == n - 1 || a[i] < a[i + 1])
{
++tempAns;
}
else
{
tempAns = 1;
}
startAns[i] = tempAns;
}
// for (int i = 0; i < n; ++i)
// {
// std::cout << endAns[i] << " ";
// }
// std::cout << std::endl;
// for (int i = 0; i < n; ++i)
// {
// std::cout << startAns[i] << " ";
// }
// std::cout << std::endl;
int ans = 1;
for (int i = 0; i < n; ++i)
{
if (i == 0 && i == n - 1)
{
// 只有一个数
ans = 1;
}
else if (i == 0)
{
// 考虑将第一个数修改为比第二个数小的数
ans = std::max(ans, startAns[i + 1] + 1);
}
else if (i == n - 1)
{
// 考虑将最后一个数修改为比倒数第二个数大的数
ans = std::max(ans, endAns[i - 1] + 1);
}
// 判断修改一个数能否让endAns与startAns接上
else if (a[i - 1] + 1 < a[i + 1])
{
ans = std::max(ans, endAns[i - 1] + startAns[i + 1] + 1);
}
else
{
// 否则至少能接上其中一个
ans = std::max(ans, endAns[i - 1] + 1);
ans = std::max(ans, startAns[i + 1] + 1);
}
}
std::cout << ans << std::endl;
}
return 0;
}
|
Problem link: 【DidaOJ】[8] Strictly Increasing Contiguous Subsegment