Codeforces Round #605 (Div. 3) (D题)

Remove One Element

You are given an array a consisting of n integers.

You can remove at most one element from this array. Thus, the final length of the array is n−1 or n.

Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.

Recall that the contiguous subarray a with indices from l to r is a[l…r]=al,al+1,…,ar. The subarray a[l…r] is called strictly increasing if al<al+1<⋯<ar.

Input
The first line of the input contains one integer n (2≤n≤2⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of a.

Output
Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element.


一道很简单的题目被我搞的那么复杂;

其实只要记录一下前缀和后缀和就行,枚举临界点;

标答:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const LL mod=998244353;
int a[N];
int dp[N][2];
int main(){
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	dp[n][1]=dp[1][0]=1;
	for(int i=2;i<=n;i++){
		if(a[i]>a[i-1]) dp[i][0]=dp[i-1][0]+1;
		else dp[i][0]=1;
	}
	for(int i=n-1;i>=1;i--){
		if(a[i]<a[i+1]) dp[i][1]=dp[i+1][1]+1;
		else dp[i][1]=1;
	}
	int mmax=0;
	for(int i=1;i<=n;i++) mmax=max(dp[i][0]+dp[i][1]-1,mmax);
	for(int i=2;i<=n;i++){
		if(a[i-1]<a[i+1]) mmax=max(mmax,dp[i-1][0]+dp[i+1][1]);
	}
	cout<<mmax<<endl;
	return 0;
}

傻逼代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const LL mod=998244353;
int a[N];
int dp[N];
int s[N];
int main(){
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		dp[i]=1;
		s[i]=1;
	}
	for(int i=1;i<=n;i++){
		if(a[i]>a[i-1]&&i!=1){
			dp[i]=dp[i-1]+1;
		}
	}
	s[n]=dp[n];
	for(int i=n-1;i>=1;i--){
		s[i]=dp[i];
		if(dp[i]<dp[i+1]){
			s[i]=s[i+1];
		}
	}
	int mmax=0;
	for(int i=1;i<=n;i++){
		mmax=max(mmax,s[i]);
		if(a[i-1]<a[i+1]){
			if(a[i]>=a[i+1]) mmax=max(mmax,s[i]+s[i+1]-1);
			else if(a[i]<=a[i-1]) mmax=max(mmax,s[i]+s[i-1]-1);
		}
	}
	cout<<mmax<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值