日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Remove One Element(贪心)

發(fā)布時間:2023/12/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Remove One Element(贪心) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

You are given an array aa consisting of nn integers.

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

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

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

Input
The first line of the input contains one integer nn (2≤n≤2?1052≤n≤2?105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the ii-th element of aa.

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

Examples
Input
5
1 2 5 3 4
Output
4
Input
2
1 2
Output
2
Input
7
6 5 4 3 2 4 3
Output
2
Note
In the first example, you can delete a3=5a3=5. Then the resulting array will be equal to [1,2,3,4][1,2,3,4] and the length of its largest increasing subarray will be equal to 44.
思路:最多刪除一個,那么我們就記錄這一個數(shù)字前一個數(shù)字和后一個數(shù)字的最長連續(xù)序列。然后貪心的取最大值。事先用dfs預(yù)處理好每一個數(shù)字最遠可以到達哪里。具體看代碼。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=2e5+100; int a[maxx]; int dp[maxx]; int np[maxx]; int n;inline void dfs(int &i,int &cnt) {if(i==n) {dp[n]=1;i+=1;return ;}int ant=cnt,j=i;if(a[i+1]>a[i]) dfs(i+=1,cnt+=1);else i+=1;dp[j]=cnt-ant+1; } inline void dfs1(int &i,int &cnt) {if(i==1){np[i]=1;i-=1;return ;}int ant=cnt,j=i;if(a[i-1]<a[i]) dfs1(i-=1,cnt+=1);else i-=1;np[j]=cnt-ant+1; } int main() {scanf("%d",&n);int _max=0;a[n+1]=a[0]=0;for(int i=1;i<=n;i++) {scanf("%d",&a[i]);}int cnt;for(int i=1;i<=n;) dfs(i,cnt=0);//dp[i]代表的是第i位開始,最長的上升序列,i++for(int i=n;i>=1;) dfs1(i,cnt=0);//np[i]代表的是第i位開始,最長的下降序列,i--for(int i=1;i<=n;i++) _max=max(_max,dp[i]);for(int i=2;i<n;i++) if(a[i-1]<a[i+1]) _max=max(_max,np[i-1]+dp[i+1]);//貪心的取最大值cout<<_max<<endl;return 0; }//1 2 3 4 5 6

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Remove One Element(贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。