题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列。

析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得,最后枚举中间值即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m; }
int f[maxn], g[maxn], a[maxn]; int main(){
while(scanf("%d", &n) == 1){
a[0] = 0;
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
f[0] = 0; f[1] = 1;
for(int i = 2; i <= n; ++i)
if(a[i] > a[i-1]) f[i] = f[i-1] + 1;
else f[i] = 1;
g[n] = 1;
for(int i = n-1; i > 0; --i)
if(a[i] < a[i+1]) g[i] = g[i+1] + 1;
else g[i] = 1;
int ans = 0;
for(int i = 1; i <= n; ++i){
ans = Max(ans, f[i]+g[i]-1);
if(i < n) ans = Max(ans, f[i]+1);
if(i > 1) ans = Max(ans, g[i]+1);
if(i > 1 && a[i] > a[i-1]) ans = Max(ans, f[i-1]+g[i]);
if(i < n && a[i] < a[i+1]) ans = Max(ans, f[i]+g[i+1]);
if(i > 1 && i < n && a[i+1] - a[i-1] > 1) ans = Max(ans, f[i-1]+g[i+1]+1);
}
printf("%d\n", ans);
}
return 0;
}

CodeForces 446A DZY Loves Sequences (DP+暴力)的更多相关文章

  1. codeforces 446A DZY Loves Sequences

    vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...

  2. Codeforces 446A. DZY Loves Sequences (线性DP)

    <题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...

  3. CodeForces - 446A DZY Loves Sequences(dp)

    题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ...

  4. CodeForces 447C DZY Loves Sequences DP

    题目:click here 题意:求给定序列更改其中一个元素后的最长连续上升子序列的长度 分析:最长的连续子序列有2种,一种是严格上升(没有更改元素)的长度加1,一种是两段严格上升的加起来. #inc ...

  5. Codeforces Round #FF 446A DZY Loves Sequences

    预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...

  6. Codeforces 447C - DZY Loves Sequences

    447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  7. codeforces 447C. DZY Loves Sequences 解题报告(446A)

    题目链接:http://codeforces.com/problemset/problem/447/C 题目意思:给出 一个 包含 n 个数的序列你,从中需要找出这个序列的最长子串,满足在里面只修改其 ...

  8. codeforces C. DZY Loves Sequences

    http://codeforces.com/contest/447/problem/C 题意:给你n个数的序列,然后让你改变其中的一个数,求得最长上升连续序列的长度值. 思路:先从左边开始求出连续递增 ...

  9. 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. admin——django自带数据库管理工具

    admin是Django自带的数据库管理工具,是一个app 在admin.py中可以自己定制类来使admin数据库管理页面展现自己想要查看的信息 models.py from django.db im ...

  2. bootloader的移植

    jz2440开发板 在介绍bootloader里边的内容的时候,需要知道的是: bootloader的引入的目的就是启动linux内核,一个简单的bootloader编写需要以下的步骤: ①初始化硬件 ...

  3. js变量类型详解

    <html> <title>js变量类型详解</title> <meta http-equiv="content-type" conten ...

  4. 九度oj 题目1070:今年的第几天?

    题目1070:今年的第几天? 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6832 解决:3779 题目描述: 输入年.月.日,计算该天是本年的第几天. 输入: 包括三个整数年(1< ...

  5. python 几种点积运算方式效率分析

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51793984 本文列举出几种pytho ...

  6. [K/3Cloud] 关于单据转换的问题

    1. 单据转换,是否支持重复下推,支持新增下推和更新下推? 答:支持重复下推,是否允许下推受以下因素: 1).源分录是否是有效状态(源单单头状态会自动影响分录,下同),例如已审核.未关闭.未作废: 2 ...

  7. 安装eclipse插件,很慢终于找到了解决的方法

    1 .除非你需要,否则不要选择"联接到所有更新站点" 在安装对话框里有一个小复选框,其标示为"在安装过程中联接到所有更新站点从而找到所需的软件."从表面上看,这 ...

  8. android源码mm时的编译错误no ruler to make target `out/target/common/obj/JAVA_LIBRARIES/xxxx/javalib.jar', needed by `out/target/common/obj/APPS/xxxx_intermediates/classes-full-debug.jar'. Stop.

    瞧见没有,就因为多了这一个反斜杠,浪费了一下午时间找问题,哭了~~~~

  9. 012 router password

    Press RETURN to get started!       Router>en Router#config t Enter configuration commands, one pe ...

  10. HTML5超科幻个人主页

    在线演示地址:http://me.cpwl.site 备用地址:http://cpwl.sinaapp.com 部分截图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...