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;
}
發佈了207 篇原創文章 · 獲贊 45 · 訪問量 8316
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章