P6364 1024 程序员节发橙子

题目链接:https://www.luogu.com.cn/problem/P6364

思路:

正反搞两次这个操作:如果元素是连续上升的,就将这个连续上升改成公差是1的等差数列,分别存在b数组和c数组,因为要同时满足b和c,所以每个对应位置取个max就可以了。

代码

#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug  freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define PI acos(-1)
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+10;
using namespace std;

int N;
int a[maxn];
int b[maxn];
int c[maxn];
int main(){
    // debug;
    ios;
    cin>>N;
    for(int i = 1;i<=N;i++) cin>>a[i];
    for(int i = 1;i<=N;i++) b[i] = 1,c[i] = 1;
    for(int i = 2;i<=N;i++){
        if(a[i] > a[i-1]) b[i] = b[i-1] +1;
        if(a[i] == a[i-1]) b[i] = b[i-1];
    }
    for(int i = N-1;i>=1;i--){
        if(a[i] > a[i+1]) c[i] = c[i+1] + 1;
        if(a[i] == a[i+1]) c[i] = c[i+1];
    }
    ll sum = 0;
    for(int i = 1;i<=N;i++) sum += max(b[i],c[i]); 
    cout<<sum<<endl;

    return 0;
}