Codeforces Round #FF (Div. 2)C.DYZ Loves Sequence
来源:互联网 发布:晾霸 好太太 知乎 编辑:程序博客网 时间:2024/05/20 16:42
一道类似求严格递增子序列的题目。这道题可以改变一个数成任意数,使得严格递增子序列增加。
*思路:两个标记数组from[i],to[i];from[i]的含义是从i出发的最长的严格递增序列长度,to[i]是到i结束的严格递增子序列长度。
如果a[i]>=a[i+1]或者a[i]<=a[i-1],长度为to[i-1]+from[i+1]+1;
DZY Loves Sequences
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.
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.
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){ int a[100005]; int from[100005];//从i出发的严格递增子序列长度 int to[100005];//到i结束的严格递增的子序列长度 int point=1; memset(from,0,sizeof(from)); memset(to,0,sizeof(to)); int i,n; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); from[n-1]=1; for(i=n-1;i>0;i--) { if(a[i]>a[i-1]) from[i-1]=from[i]+1; else from[i-1]=1; }//预处理,类似dp,找个状态。 to[0]=1; for(i=0;i<n-1;i++) { if(a[i]<a[i+1]) to[i+1]=to[i]+1; else to[i+1]=1; }//预处理 if(n==1||n==2) printf("%d\n",n);//特殊情况 else { if(a[0]>=a[1])//特殊情况 point=max(point,from[1]+1); if(a[n-1]<=a[n-2]) point=max(point,to[n-2]+1);//特殊情况 <span style="white-space:pre"></span>else{ for(i=1;i<n-1;i++) { if(a[i]<=a[i-1]) { if(a[i+1]-a[i-1]>=2) <span style="white-space:pre"></span>point=max(point,to[i-1]+from[i+1]+1); else { point=max(point,from[i]+1); point=max(point,to[i-1]+1); } } else if(a[i]>=a[i+1]) { if(a[i+1]-a[i-1]>=2) point=max(point,to[i-1]+from[i+1]+1); else point=max(point,to[i]+1); } else point=max(from[i]+to[i]-1,point); }//对非特殊数据进行处理 } printf("%d\n",point); } return 0;}
- Codeforces Round #FF (Div. 2)C.DYZ Loves Sequence
- Codeforces Round #FF (Div. 2) A.DYZ Loves Hash
- Codeforces Round #FF (Div. 2) B.DYZ Loves Strings
- Codeforces Round #FF (Div. 2):C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) Problem C DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C - DZY Loves Sequences
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2/C)/Codeforces446A_DZY Loves Sequences(DP)
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
- Codeforces Round #FF (Div. 1) C - DZY Loves Fibonacci Numbers
- Codeforces Round #FF (Div. 二):C. DZY Loves Sequences
- Error pulling origin: error: Your local changes to the following files would be overwritten by merge
- 按键驱动_查询方式
- Fiddler 教程
- 系统时间——年月日——使用宝典
- 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误。
- Codeforces Round #FF (Div. 2)C.DYZ Loves Sequence
- 如何学习C语言最有效
- How to Keep Thread-Safe When Queuing Your Data
- 织梦DedeCMS自带采集规则只能够选择文章和图集模型的修改
- C#中字符,字符串的大小写转换
- 按照时间戳中的day 聚合排序
- 【健康生活】优秀程序员必备的15大技能
- Tomcat UTF-8 编码设置
- ubuntu 周期性任务管理 --crontab