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

析: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. ACdream 1063 字典树

    ACdream 1063 字典树 平衡树 神奇的cxlove有一颗平衡树,其树之神奇无法用语言来描述 OrzOrz. 这棵树支持3种操作: 1.加入一个数到树中,维护平衡树的合法性: 2.给一个数X, ...

  2. Swagger UI教程

    文档源地址 http://www.68idc.cn/help/makewebs/qitaasks/20160621620667.html Swagger-UI本身只提供在线测试功能,要集成它还需要告诉 ...

  3. Jmeter关联,正则表达式提取器使用2

    正则表达式的用处很多,最基础的用法 1,断言 2,传参(关联) 例子 1.http请求 2正则表达式提取,想要提取列表列中id,一遍打开列表页 如果是1,每次就会取相同的值!匹配数字的权限高于模板$0 ...

  4. MBP 2018 & Mac keyboard shortcuts

    MBP 2018 & Mac keyboard shortcuts https://support.apple.com/en-us/HT201236 delete key === Contro ...

  5. Netbeans工具使用记录

     #自动换行设置 工具->选项->编辑器->格式设置

  6. msp430入门编程0

    msp430单片机最小系统 msp430入门学习 msp430入门编程

  7. Ubuntu 12.04 之 虚拟主机的配置

    Ubuntu 12.04 之 虚拟主机的配置 (1)打开etc/hosts文件 增加: 127.0.0.1 study.ubuntu.com 127.0.0.1 hello.ubuntu.com 12 ...

  8. AtCoder Grand Contest 011 E - Increasing Numbers(灵性乱搞)

    题意: 当一个整数高位数字总不小于低位数字,或者说写成字符串之后单调不下降,称之为上升数.求一个整数最少能表示为多少个上升数的和.(n<=1e500000) 分析: 考虑那些不下降的数字,一定可 ...

  9. 【c++】【转】c++中的explicit关键字

    http://www.cnblogs.com/chio/archive/2007/09/17/895263.html c++中的explicit关键字用来修饰类的构造函数,表明该构造函数是显式(调用) ...

  10. Centos7 samba 匿名共享 简单config

    安装Samba yum install samba samba-client samba-common -y 备份原始的Samba配置文件: mv /etc/samba/smb.conf /etc/s ...