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;
}