CF - 447C. DZY Loves Sequences - 动态规划
2017-03-02 22:36
197 查看
1.题目描述:
C. DZY Loves Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
DZY 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.
Input
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Examples
input
output
Note
You can choose subsegment a2, a3, a4, a5, a6 and
change its 3rd element (that is a4)
to 4.
2.题意概述:
给定一个数列,你可以不改变或者对其中一个数+1,问你最长上升子序列
3.解题思路:
将原数组分割成递增的子串,记录下每个子串的开始和结束位置,以及长度。
接下来要分几种情况讨论:
1.相邻的两个子串改变一个数字之后,可以合并形成新的递增子串。
2.相邻的3个子串,中间子串长度为1,改变中间的数字后可以形成新的递增子串。
3.相邻的子串不能合并形成新的递增子串,但是可以在原串的基础上,得到一个长度增加1的新的递增子串(在子串开头位置前有数字,或是结束位置后有数字)。
4.AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 100100
using namespace std;
int a[maxn];
struct node
{
int l, r, len;
}p[maxn];
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
memset(p, 0, sizeof(p));
memset(a, 0, sizeof(a));
int cnt = 0, ans;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
if (i == 0)
{
p[cnt].len++;
p[cnt].l = p[cnt].r = i;
continue;
}
if (a[i] <= a[i - 1])
cnt++;
if (p[cnt].len == 0)
p[cnt].l = i;
p[cnt].r = i;
p[cnt].len++;
}
if (p[0].len < n)
ans = p[0].len + 1;
else
ans = p[0].len;
for (int i = 1; i <= cnt; i++)
{
ans = max(ans, p[i].len + 1);
if (a[p[i].l] > a[p[i - 1].r - 1] + 1 || a[p[i].l + 1] > a[p[i - 1].r] + 1)
ans = max(ans, p[i].len + p[i - 1].len);
if (i >= 2 && p[i - 1].len == 1 && a[p[i].l] > a[p[i - 2].r + 1])
ans = max(ans, p[i].len + p[i - 1].len + p[i - 2].len);
}
printf("%d\n", ans);
}
return 0;
}
C. DZY Loves Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
DZY 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.
Input
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Examples
input
6 7 2 3 1 5 6
output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and
change its 3rd element (that is a4)
to 4.
2.题意概述:
给定一个数列,你可以不改变或者对其中一个数+1,问你最长上升子序列
3.解题思路:
将原数组分割成递增的子串,记录下每个子串的开始和结束位置,以及长度。
接下来要分几种情况讨论:
1.相邻的两个子串改变一个数字之后,可以合并形成新的递增子串。
2.相邻的3个子串,中间子串长度为1,改变中间的数字后可以形成新的递增子串。
3.相邻的子串不能合并形成新的递增子串,但是可以在原串的基础上,得到一个长度增加1的新的递增子串(在子串开头位置前有数字,或是结束位置后有数字)。
4.AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 100100
using namespace std;
int a[maxn];
struct node
{
int l, r, len;
}p[maxn];
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
memset(p, 0, sizeof(p));
memset(a, 0, sizeof(a));
int cnt = 0, ans;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
if (i == 0)
{
p[cnt].len++;
p[cnt].l = p[cnt].r = i;
continue;
}
if (a[i] <= a[i - 1])
cnt++;
if (p[cnt].len == 0)
p[cnt].l = i;
p[cnt].r = i;
p[cnt].len++;
}
if (p[0].len < n)
ans = p[0].len + 1;
else
ans = p[0].len;
for (int i = 1; i <= cnt; i++)
{
ans = max(ans, p[i].len + 1);
if (a[p[i].l] > a[p[i - 1].r - 1] + 1 || a[p[i].l + 1] > a[p[i - 1].r] + 1)
ans = max(ans, p[i].len + p[i - 1].len);
if (i >= 2 && p[i - 1].len == 1 && a[p[i].l] > a[p[i - 2].r + 1])
ans = max(ans, p[i].len + p[i - 1].len + p[i - 2].len);
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- J-- DZY Loves Sequences(CF-447C
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
- CF Round #FF (Div. 2) C DZY Loves Sequences_DP
- CodeForces-447C DZY Loves Sequences
- codeforces 447C DZY Loves Sequences(最长上升子序列变体)
- CodeForces 447C DZY Loves Sequences (dp 子序列)
- Codeforce466A——贪心——DZY Loves Sequences
- ACM篇:CF 446B -- DZY Loves Modification
- CF 445A(DZY Loves Chessboard-BW填充)
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)446A - DZY Loves Sequences(最长上升子序列变形)
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF (Div. 1)——A贪心——DZY Loves Sequences
- E--DZY Loves Chemistry(CF-445B
- CF 445B(DZY Loves Chemistry-求连通块)
- CF 444B(DZY Loves FFT-时间复杂度)
- codeforces#FF(div2) DZY Loves Sequences
- Codeforces Round #FF(255) C. DZY Loves Sequences (LIS升级)
- CF A. DZY Loves Hash