CodeForces 447C DZY Loves Sequences (dp 子序列)
来源:互联网 发布:怎么检查网络是否连接 编辑:程序博客网 时间:2024/05/29 18:24
因为change at most one number (change one number to any integer you want)
所以找到左边递增的最大区间长度
然后找到右边递增的最大区间长度
如果最大长度就是n,那么说明原来就是升序排好了的。
注意以下数据:
5
1 2 2 3 4
输出: 4
有个细节见下方代码
#include<stdio.h>#include<iostream>#include<math.h>#include<string.h>#include<iomanip>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<deque>#include<functional>#include<iterator>#include<vector>#include<list>#include<map>#include<queue>#include<set>#include<stack>#define CPY(A, B) memcpy(A, B, sizeof(A))typedef long long LL;typedef unsigned long long uLL;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const double EPS = 1e-9;const double OO = 1e20;const double PI = acos (-1.0);const int dx[] = {-1, 0, 1, 0};const int dy[] = {0, 1, 0, -1};using namespace std;const int maxn=1e5+10;struct AA { int left/*left length*/,right,element;};AA dp[maxn];int main() { int n; scanf ("%d",&n); memset (dp,0,sizeof (dp) ); for (int i=1; i<=n; i++) { scanf ("%d",&dp[i].element); } if (n==1) {printf ("1\n");}/**/ else { int ans=0; dp[1].left=1; for (int i=2; i<=n; i++) { dp[i].left= (dp[i].element>dp[i-1].element?dp[i-1].left+1/*length add 1*/:1); ans=max (ans,dp[i].left);//大于左边的,那么就是左边长度再加1 } dp[n].right=1; for (int i=n-1; i>=1; i--) { dp[i].right= (dp[i+1].element>dp[i].element?dp[i+1].right+1:1); ans=max (ans,dp[i].right); } if (ans==n) { printf ("%d",n); }//等于n意味着之前顺序就是排好的 else { for (int i=1; i<=n; i++) { if (dp[i+1].element>dp[i-1].element+1) {//must be dp[i+1]>dp[i-1]+1,because: 1 ? 2 and 1 ? 1 ans=max (ans,dp[i-1].left+dp[i+1].right+1); } else { ans=max (ans,max (dp[i].left+1,dp[i].right+1) ); } } printf ("%d\n",ans); } } return 0;}
0 0
- CodeForces 447C DZY Loves Sequences (dp 子序列)
- codeforces 447C DZY Loves Sequences(最长上升子序列变体)
- CodeForces 447C###C. DZY Loves Sequences
- CodeForces-447C DZY Loves Sequences
- Codeforces 446A DZY Loves Sequences【dp】
- 447 C. DZY Loves Sequences
- C. DZY Loves Sequences
- C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)446A - DZY Loves Sequences(最长上升子序列变形)
- J-- DZY Loves Sequences(CF-447C
- Codeforces Round #FF (Div. 2) C - DZY Loves Sequences (DP)
- Codeforces 446A —— DZY Loves Sequences(DP)
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
- CodeForces 446A DZY Loves Sequences(DP)
- DZY Loves Sequences 预处理 dp
- Codeforces Round #FF (Div. 2):C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) Problem C DZY Loves Sequences
- 如何避免野指针?
- MySQL Error 1093(HY000) Update不支持子查询的问题
- android simpledraweeview 获取已经加载的图片bitmap
- 使用masm5.0编译时提示incorrect ms-dos version怎么办?
- uc/os-iii学习笔记-任务调度
- CodeForces 447C DZY Loves Sequences (dp 子序列)
- hdu3849 Tarjan求无向图的割边+map
- "应用程序无法正常启动(0xc000007b)。请单击 确定 关闭应用程序。"解决方法
- 虚拟机中LINUX环境的oracle中文乱码的问题
- scala 借贷模式(loan pattern)通用实现
- 模块化的JavaScript开发的优势在哪里
- 关于算法的一点思考。。。
- .NET CORE实践(1)--Ubuntu下的Hello World
- STM32 FSMC控制器