Codeforces Round #FF (Div. 2) C - DZY Loves Sequences (DP)

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.

Sample test(s)

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.

题意: 寻找长度最大的连续子串满足 子串严格单调递增,前提是你可以修改其中的一个数,把它变成一个任意的数,并且只能修改一次。

可以考虑将 已每个数作为一个连续的串的最后一个元素,那么a[i]就为该串的最大值,找到往前能扩展出的个数,保存在l[i],往右也是类似。

预处理完成之后考虑第i个位置,如果a[+1] - a[i-1] >1,就可以修改i这个位置,(当然也可以不修改,修改或不修改要看是否a[i-1]<a[i]<a[i+1]).

那么更新答案l[i-1]+r[i+1]+1. 否则可以修改i这个位置,但前后两段是连不起来的,那么只能更新答案 max(l[i-1],r[i+1])+1 。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cmath>
#define mem(f) memset(f,0,sizeof(f))
#define M 100005
#define mod 10000007
#define MAX 0X7FFFFFFF
#define maxn 500050
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;

int n, a[maxn], l[maxn], r[maxn];

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];

    l[1] = 1;
    for(int i = 2; i <= n; i++)
        if(a[i] > a[i-1]) l[i] = l[i-1] + 1;
        else l[i] = 1;

    r[n] = 1;
    for(int i = n-1; i > 0; i--)
        if(a[i+1] > a[i]) r[i] = r[i+1] + 1;
        else r[i] = 1;

    int ans = r[2] + 1;
    ans = max(ans, l[n-1] + 1);
    for(int i = 2; i < n; i++)
        if(a[i+1] - a[i-1] > 1) ans = max(ans, l[i-1] + r[i+1] + 1);
        else ans = max(ans, max(r[i+1], l[i-1])+1);

    cout << ans << endl;
    return 0;
}



时间: 2024-10-10 09:09:23

Codeforces Round #FF (Div. 2) C - DZY Loves Sequences (DP)的相关文章

DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + 1 2. l[i-1] + 1 3. r[i+1] + 1 //修改a[i] */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN =

Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

原题链接:http://codeforces.com/problemset/problem/446/A 题意:给一个长度为n的序列,最多可以修改一个位置的数,求最长连续上升子序列. 题解:当a[i+1] > a[i-1]+2的时候,可以通过改变a[i]的值来使前后两段合并,反之,分别考虑a[i]作为左边那段最长的和右边那段最长的. #include <cstdio> #include <cstring> #include <algorithm> #include

Codeforces Round #FF(255) DIV2 C - DZY Loves Sequences

A - DZY Loves Hash 水题,开辟一个数组即可 #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int main(){ int p,n; cin >> p >> n; vector<bool> buckets(302,false); bool flag = fal

Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的次序将1-n种试剂滴入试管,如果正在滴入的试剂能与已经滴入 的试剂反应,那么危险数*2,否则维持不变.问最后最大的危险系数是多少. 分析:其实这个题根本不用考虑倒入的顺序,只是分块就行,结果就是每个子集里元素的个数-1 和  的2的幂. 1 #include <iostream> 2 #inclu

Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the follow

Codeforces Round #FF (Div. 2) A. DZY Loves Hash

DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In

Codeforces Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列

链接:http://codeforces.com/problemset/problem/447/D 题意:一个n*m的矩阵.能够进行k次操作,每次操作室对某一行或某一列的的数都减p,获得的得分是这一行或列原来的数字之和.求N次操作之后得到的最高得分是多少. 思路:首先分别统计每行和每列的数字和. 进行的k次操作中,有i次操作是对行进行操作,剩余k-i次操作是对列进行操作. 首先在操作中忽略每次操作中行对列的影响,然后计算列的时候,最后能够计算出,总共的影响是i*(k-i)*p. 找出对于每一个i

Codeforces Round #FF (Div. 2) E. DZY Loves Fibonacci Numbers(斐波那契的定理+线段树)

/* 充分利用了菲波那切数列的两条定理: ①定义F[1] = a, F[2] = b, F[n] = F[n - 1] + F[n - 2](n≥3). 有F[n] = b * fib[n - 1] + a * fib[n - 2](n≥3),其中fib[i]为斐波那契数列的第 i 项. ②定义F[1] = a, F[2] = b, F[n] = F[n - 1] + F[n - 2](n≥3). 有F[1] + F[2] + -- + F[n] = F[n + 2] - b 这题还有一个事实,

Codeforces Round #267 (Div. 2) C. George and Job (dp)

wa哭了,,t哭了,,还是看了题解... 8170436                 2014-10-11 06:41:51     njczy2010     C - George and Job             GNU C++     Accepted 109 ms 196172 KB 8170430                 2014-10-11 06:39:47     njczy2010     C - George and Job             GNU C