CF - 447C. DZY Loves Sequences - 动态规划

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
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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Process: com.example.dzy, PID: 26008 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dzy/com.example.dzy.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.example.dzy.Fragment_1.<init>(Fragment_1.java:44) at com.example.dzy.NavigationActivity.initTab(NavigationActivity.java:39) at com.example.dzy.NavigationActivity.onCreate(NavigationActivity.java:27) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值