Codeforces 446A DZY Loves Sequences【dp】
来源:互联网 发布:淘宝快递合作价格表 编辑:程序博客网 时间:2024/05/16 07:00
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 sequencea. 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 containsn integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
67 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.
题目大意:
给你一个长度为N的序列,现在允许你改变其中任意一个元素的值(改变成的值当然也是任意的),让你从中找到一个连续递增子序列,求其最长的长度。
思路:
1、考虑dp,设定dp【i】【2】:
其dp【i】【0】表示以a【i】结尾,并且还没有改变元素值时的连续递增子序列的最大长度。
其dp【i】【1】表示以a【i】结尾,并且已经过改变元素值时的连续递增子序列的最大长度。
2、那么接下来考虑解决问题思路:
①首先我们将这个问题分成两种情况,第一种是将其中任意一个元素的值变大,第二种是将其中任意一个元素值变小。
②那么我们进行两次dp,第一次解决将元素值变大的情况,第二次解决将元素值变小的情况。
③那么最终答案就是两次dp之后维护最大值即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int dp[150000][2];int pos[150000];int a[150000];int main(){ int n; while(~scanf("%d",&n)) { memset(a,0,sizeof(a)); memset(pos,-1,sizeof(pos)); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } //先考虑让这个数变大。 memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { if(i==0) { dp[i][0]=1; } else { if(a[i]>a[i-1]) { dp[i][0]=max(dp[i][0],dp[i-1][0]+1); if(pos[i-1]==-1) { dp[i][1]=max(dp[i][1],dp[i-1][1]+1); } else { if(a[i]>pos[i-1]) { dp[i][1]=max(dp[i][1],dp[i-1][1]+1); } } } else { dp[i][0]=1; dp[i][1]=max(dp[i-1][0]+1,dp[i][1]); pos[i]=a[i-1]+1; } } } int output=0; for(int i=1;i<=n;i++) { output=max(output,max(dp[i][0],dp[i][1])); } memset(dp,0,sizeof(dp)); memset(pos,-1,sizeof(pos)); for(int i=1;i<=n;i++) { if(i==1) { dp[i][0]=1; dp[i][1]=1; pos[i]=a[i+1]-1; } else { if(a[i]>a[i-1]) { dp[i][0]=max(dp[i][0],dp[i-1][0]+1); dp[i][1]=max(dp[i][1],dp[i-1][1]+1); } else { dp[i][0]=1; if(pos[i-1]==-1) { if(i-2>=0&&a[i]-a[i-2]>=2)dp[i][1]=dp[i-2][0]+2; else dp[i][1]=2; } else dp[i][1]=max(dp[i][1],dp[i-1][1]+1); } } } for(int i=1;i<=n;i++) { output=max(output,max(dp[i][0],dp[i][1])); } printf("%d\n",output); }}
- Codeforces 446A DZY Loves Sequences【dp】
- Codeforces 446A —— DZY Loves Sequences(DP)
- CodeForces 446A DZY Loves Sequences(DP)
- Codeforces 446A DZY Loves Sequences
- CodeForces 446A DZY Loves Sequences
- codeforces 446A DZY Loves Sequences
- CodeForces 446-A.DZY Loves Sequences
- codeforces-446A-DZY Loves Sequences
- CodeForces 446A DZY Loves Sequences
- Codeforces Round #FF 446A DZY Loves Sequences
- CF 446A DZY Loves Sequences
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
- CodeForces 447C DZY Loves Sequences (dp 子序列)
- DZY Loves Sequences 预处理 dp
- Codeforces Round #FF (Div. 2)446A - DZY Loves Sequences(最长上升子序列变形)
- codeforces#FF(div2) DZY Loves Sequences
- CodeForces-447C DZY Loves Sequences
- Xtreme 10.0 - Flower Games
- android listview嵌套listview只显示一行解决方法
- Linux平滑编译升级php5.5至5.6
- web前端 前后页面跳转传值
- 设计模式笔记——(二:建造者模式 Builder Pattern)
- Codeforces 446A DZY Loves Sequences【dp】
- 字符串匹配——简单匹配,KMP,分析讲解
- 在cmd使用mongoDB的指令
- Java安全管理器——SecurityManager
- 搞懂JAVA集合类--集合类框架(一)
- Codeforces Gym 101061 F(暴力搜索做法)
- 文件类型检测
- Object c/swift,java,c/c++在32位和64位各个平台上基本数据类型 所占有的字节数
- [LeetCode]--31. Next Permutation