CF446A 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.

Examples

Input

Copy

67 2 3 1 5 6

Output

Copy

5

Note

You can choose subsegment a2,?a3,?a4,?a5,?a6 and change its 3rd element (that is a4) to 4.

问最多修改一个数字,序列可获得地最大严格递增字段长度为多大;

考虑dp;

dp1 表示以 i 位置结尾的最长子段长度;

dp2 表示以 i 位置开头的最长子段长度;

特判一下当 n=1时,长度为1;

考虑拼接:当 x[ i+1 ]>=2+ x[ i-1 ]时,那么改变 x[ i ]即可拼接子段

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
    ll x = 0;
    char c = getchar();
    bool f = false;
    while (!isdigit(c)) {
        if (c == ‘-‘) f = true;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? -x : x;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1; y = 0; return a;
    }
    ans = exgcd(b, a%b, x, y);
    ll t = x; x = y; y = t - a / b * y;
    return ans;
}
*/

int n;
int x[maxn];

int main() {
    //ios::sync_with_stdio(0);
    cin >> n;
    vector<int>dp1(maxn, 1);
    vector<int>dp2(maxn, 1);
    for (int i = 0; i <= n; i++)dp1[i] = dp2[i] = 1;
    for (int i = 0; i < n; i++)rdint(x[i]);
    if (n < 2) {
        cout << 1 << endl; return 0;
    }
    for (int i = 1; i < n; i++)
        dp1[i] = (x[i] > x[i - 1]) ? dp1[i - 1] + 1 : 1;
    for (int i = n - 2; i >= 0; i--)
        dp2[i] = (x[i + 1] > x[i]) ? dp2[i + 1] + 1 : 1;
    int ans = 0;
    for (int i = 1; i < n; i++)ans = max(ans, dp1[i - 1] + 1);
    for (int i = 0; i < n; i++)ans = max(dp2[i + 1] + 1, ans);
    for (int i = 1; i <= n - 1; i++) {

        if (x[i + 1] - x[i - 1] >= 2) {
            ans = max(ans, dp1[i - 1] + 1 + dp2[i + 1]);
        }
    }
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/zxyqzy/p/10215200.html

时间: 2024-11-23 09:04:30

CF446A DZY Loves Sequences 简单dp的相关文章

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, su

Codeforces 447 C DZY Loves Sequences【DP】

题意:给出一列数,在这个序列里面找到一个连续的严格上升的子串,现在可以任意修改序列里面的一个数,问得到的子串最长是多少 看的题解,自己没有想出来 假设修改的是a[i],那么有三种情况, 1.a[i]>a[i-1],那么求出向左能够延伸的最长的长度 2.a[i]<a[i-1],那么求出向右能够延伸的最长的长度 3.如果修改的这个数刚好夹在两个数的中间,这种情况比上面两种都优, 即为a[i-1]<a[i+1]-1,求出左右能够延伸的最长的长度 然后因为a[i]本身还没有算进去,所以求出最大值

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 446A DZY Loves Sequences

codeforces   446A   DZY Loves Sequences         题目链接:http://codeforces.com/problemset/problem/446/A 题目大意:给出一个定长为n的数列a,问改动当中一个数后.可能出现的最长严格上升子段的长度是多少. 题目分析:先不考虑"改动当中一个数"这个条件,这样问题就简单多了,从前到后遍历计数就可以(定义一个数组inc[]长度同a,初始化全部点为1,遍历假设当前点a[i]>a[i-1]就置inc

Codeforces Round #FF(255) C. DZY Loves Sequences (LIS升级)

题目:C. DZY Loves Sequences (LIS升级) 题意: 在n个数中,最多改变一个数字,并求能够达到的最长严格上升子序列(连续)长度 分析: 考虑第i个数,能否改变后拼接前后两个字串,并维护当前最大值 状态: left[i]:  表示以i为终点的最长严格上升子序列长度 right[i]: 表示以i为起点的最长严格上升子序列长度 dp[i]:   表示改变第i个数后,拼接前后字串的长度 转移方程:       dp[i] = max{left[i-1] + right[i+1] 

Codeforces 447C - DZY Loves Sequences

447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll long long const int INF=0x3f3f3f3f; const int N=1e5+5; int a[N]; int f[N],g[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; f

[2016-04-13][codeforces][447][C][DZY Loves Sequences]

时间:2016-04-13 23:39:47 星期三 题目编号:[2016-04-13][codeforces][447][C][DZY Loves Sequences] 题目大意:给定一串数字,问改变其中一个数字之和,最长能得到多长的严格增加的子串 分析: 维护每个数字往左和往右能延续多长(严格减,增),然后枚举每个点, 如果这个点已经在一个严格增加的序列中,那么ans =min(n, max(ans , l[i] + r[i] + 1)) 即左右两边延伸之后,改变后面非递增的一个数字 注意这

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#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l