The 8th "Turing Cup" NEUQ-ACM Programming Contest Individual Competition

Contest link

Question C
Regarding the definition of sub-array, if you can obtain array a from array b by deleting several elements from the beginning and the end respectively (it can be zero or all, and the number of deletions before and after it does not have to be the same), then array a is called array b The sub-array. (The sub-array contains the original array, but does not contain the empty string)
That is, the sub-array is a continuous
idea: all sub-strings of a non-descending array are in non-descending order, as long as the given array is divided into several non-descending parts (each part To be as long as possible), that is, disconnect from each place, and then calculate the number of substrings. For an array of length n, the number of substrings is traversed and then added.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100010;
int a[N], f[N];
int main()
{
    
    
    int n;
    cin >> n;
    f[1] = 1;
    long long ans = 0;
    for(int i=1; i<= n; i++)
    {
    
    
        cin >> a[i];
        f[i] = 1;
        if(a[i] >= a[i-1] && i > 1)
            f[i] = f[i-1] + 1;
        ans += f[i];

    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/cosx_/article/details/113445019