您的位置:首页 > 其它

CF#FF (Div. 2) C.

2014-07-23 20:50 323 查看
C. DZY Loves Sequences
time limit per test
1 second

memory limit per test
256 megabytes

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.

解题思路:

感觉我又水了·····不会做·····问tyh大牛之后稍稍懂了点。题意就是说给一个数n,然后输入n个数代表一个序列,

求最大的严格的单调递增的子序列(允许子序列其中一个数通过改变自身值来来变成严格单增序列)。

开一个up数组,一个down数组。up数组记录以当前数字开头向右的上升序列长度,down数组记录以当前数字

结尾向左的上升序列长度。然后从1扫到n - 2,对于每个位置 i ,如果它是通过改变值来实现上升子序列的,那a[ i - 1]

和a[ i + 1]不能相差小于1,因为严格单增,不能出现前后两个元素相等的情况。只理解了这么多······后来那个max没想

通,以后想通了再补充······



AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)
#define Display(A, n, m) {                      \
    REP(i, n){                                  \
        REP(j, m) cout << A[i][j] << " ";       \
        cout << endl;                         \
    }                                           \
}

#define Display_1(A, n, m) {                    \
    REP_1(i, n){                                \
        REP_1(j, m) cout << A[i][j] << " ";     \
        cout << endl;                         \
    }                                           \
}
typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VD;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef map<LL, int> MLI;
typedef map<DB, int> MDI;
typedef pair<int, int> PII;
typedef pair<int, bool> PIB;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0);
#define N 1000010
int a
 , dw
 , up
;
int i , j , k;
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int n;
    while(~scanf("%d",&n))
    {
        REP(i , n)
            scanf("%d",&a[i]);
        if(n == 1)
        {
            printf("1\n");
            continue;
        }
        up[n - 1] = 1;
        dw[0] = 1;
        FOR(i , 1 , n)
        {
            if(a[i - 1] < a[i])
                dw[i] = dw[i - 1] + 1;
            else
                dw[i] = 1;
        }
        DWN_1(i , n - 2 , 0)
        {
            if(a[i + 1] > a[i])
                up[i] = up[i + 1] + 1;
            else
                up[i] = 1;
        }
        int maxn = max(dw[n - 2] , up[1]) + 1;
        FOR(i , 1 , n - 1)
        {
            if(a[i - 1] + 1 < a[i + 1])
                maxn = max(maxn , dw[i - 1] + up[i + 1] + 1);
            else
                maxn = max(maxn , max(dw[i - 1] + 1 , up[i + 1] + 1));
        }
        printf("%d\n",maxn);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: