-
[1667] Hkhv Loves Sequences
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
- Hkhv has a sequence a, consisting of n integers.
We‘ll call a sequence ai,ai+1,...,aj (1<= i<= j<= n) a subsegment of the sequence a. The value (j-i+1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
- 输入
- The first line contains integer n (1 <=n <= 10^5). The next line contains n integers a1,a2,...,an (1 <=ai <= 10^9).
- 输出
- In a single line print the answer to the problem — the maximum length of the required subsegment.
- 样例输入
-
6 7 2 3 1 5 6
- 样例输出
-
5
- 提示
-
You can choose subsegment a2,a3,a4,a5,a6 and change its 3rd element (that is a4) to 4.
- 来源
-
Recoder
好开心啊,居然AC了。不过题目表述不太清楚吧?改的数可以改成负数吗?(默认为可以就AC了)
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct node { int from,to; int l; }data[100011]; int main() { int u; int n; int num; int a[100011]; int ans; scanf ("%d",&u); while (u--) { scanf ("%d",&n); for (int i = 1 ; i <= n ; i++) scanf ("%d",&a[i]); num = 1; ans = 1; data[num].from = 1; data[num].l = 1; for (int i = 2 ; i <= n ; i++) { if (a[i] > a[i-1]) { data[num].l++; data[num].to = i; ans = max (ans,data[num].l); } else { num++; data[num].from = i; data[num].to = i; data[num].l = 1; } } if (ans == n || ans == n-1) //特判一下 { printf ("%d\n",n); continue; } // if (ans == n - 1) //题目不清晰,不知道是否可以把该数改为负数 // { // if ((a[1] < a[2]) || (a[1] >= a[2] && a[2] != 0)) // printf ("%d\n",n); // else // printf ("%d\n",ans); // continue; // } ans++; for (int i = 1 ; i < num ; i++) { if (data[i+1].l == 1 || data[i].l == 1) //子串长度为1挺麻烦的,单独处理下 { ans = max (ans , data[i+1].l + 1); ans = max (ans , data[i].l + 1); if (data[i+1].l == 1 && i != num-1) { if (a[data[i+1].to-1] + 1 < a[data[i+2].from]) ans = max (ans , data[i].l + data[i+2].l + 1); } } else { if (a[data[i].to-1] + 1 < a[data[i+1].from]) //修改前一个子串的最后一个数 ans = max (ans , data[i].l + data[i+1].l); if (a[data[i+1].from-1] + 1 < a[data[i+1].from+1]) //修改后一个子串的第一个数 ans = max (ans , data[i].l + data[i+1].l); } } printf ("%d\n",ans); } return 0; }
时间: 2024-10-06 23:04:02