cf446A DZY Loves Sequences
1 second
256 megabytes
standard input
standard output
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.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
- 6
- 7 2 3 1 5 6
- 5
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
题意是有一串序列,可以把其中的一个数修改成任意值,求这样改完以后最长上升子序列长度
三维dp:前i个、是否用掉第i个、是否和以前的相连的最长长度
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<algorithm>
- #include<cmath>
- #define inf 0x7fffffff
- #define ll long long
- using namespace std;
- inline ll read()
- {
- ll x=0,f=1;char ch=getchar();
- while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
- while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
- return x*f;
- }
- int n,ans;
- int a[100005],f[100005][2][2];//used? last?
- int main()
- {
- n=read();
- for(int i=1;i<=n;i++)
- a[i]=read();
- for(int i=1;i<=n;i++)
- {
- f[i][0][0]=f[i][1][1]=1;
- if(i!=1)f[i][1][0]=2;
- if(a[i]>a[i-1])
- {
- f[i][0][0]=max(f[i][0][0],f[i-1][0][0]+1);
- f[i][1][0]=max(f[i][1][0],f[i-1][1][0]+1);
- f[i][1][1]=max(f[i][1][1],f[i-1][0][0]+1);
- }
- else
- {
- f[i][1][1]=max(f[i][1][1],f[i-1][0][0]+1);
- }
- if(i!=1&&a[i]>a[i-2]+1)f[i][1][0]=max(f[i][1][0],f[i-1][1][1]+1);
- ans=max(ans,f[i][0][0]);
- ans=max(ans,f[i][1][0]);
- ans=max(ans,f[i][1][1]);
- }
- printf("%d",ans);
- }
cf446A DZY Loves Sequences的更多相关文章
- 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 ...
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...
- Codeforces Round #FF (Div. 2):C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #FF 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- [CodeForces - 447C] C - DZY Loves Sequences
C - DZY Loves Sequences DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai ...
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...
- 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] + ...
- C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- poj 2773 利用欧拉函数求互质数
题意:找到与n互质的第 k个数 开始一看n是1e6 敲了个暴力结果tle了,后来发现k达到了 1e8 所以需要用到欧拉函数. 我们设小于n的 ,与n互质的数为 (a1,a2,a3.......a(p ...
- Android数据库信息显示在listview上
Key Points: 1.使用SimpleCursorAdapter将Android数据库信息显示在listview上 adapter = new SimpleCursorAdapter(this, ...
- XAMPP安装及配置注意事项
1.下载对应版本以后,解压安装 2.设置环境变量中的path,即D:\xampp\mysql\bin 3.设置监听端口 4.解决端口冲突问题 5.各种测试网址注意事项 由于很晚了,先记录下来,明天补充 ...
- 在sae配置django项目
1:使用sae开发必须有sae帐号,自己注册吧: 2:在sae上新建立一个python项目: 3:建立完成之后新建一个版本,即版本1: 4:获取svn的地址在eclipse中check下来: 5:注意 ...
- samba服务器详细配置(非域模式)
组成Samba运行的有两个服务,一个是SMB,另一个是NMB:SMB是Samba 的核心启动服务,主要负责建立Samba服务器与Samba客户机之间的对话,验证用户身份并提供对文件和打印系统的访问,只 ...
- docke 网络配置
一,docker 的bridge模式是和vmware中的nat模式类似的,但是如果想要弄成和vmwae中的bridge怎么办呢? 说明,bridge模式获取的Ip是与宿主机的ip是出于同一个网段的. ...
- python - 类的特殊成员
class Foo: #构造方法 def __init__(self,name,age): pass self.name = name self.age = age def __str__(self) ...
- 小结: Async & Await
新项目组用到Async & Await, 关于Await会不会新开不开线程,遇到什么情况会新开线程的问题网上查了很多资料都没看到直观的解释.现简单总结一下. 直接上代码: namespace ...
- c#利用VM_COPYDATA实现进程间通信
c#进程间的通信方式很多种,只会这种,感觉比较简单.不懂原理,能用就行. 假设有两个程序:server(主进程),client(子进程) 1.server端: /*定义一个结构体,用来接收从子进程传过 ...
- 返回ipv 地址
//返回ipv 地址 public static string GetIP4Address() { string IP4Address = String.Empty; foreach (IPAddre ...