Hey Rhizomys

H after eating a small roast bamboo rat, I feel very delicious, but also move the mind Rhizomys own culture, there are a lot of bamboo near the right home, then built a home next to the farm, he farms out of every interval of N Room and compiled a good number, raising a bamboo rat in each compartment. But the characteristics Rhizomys very strange, at feeding time, if you see a large bamboo rat body weight, about adjacent compartments have Rhizomys lighter weight than it has fed more food, great mood will Rhizomys depressed and affect its health. 
For scientific and efficient breeding, two rules to follow when fed small decisions H:
1, at least to each bamboo rat fed a bamboo
2, adjacent bamboo rat, if a different weight, the higher the weight must be fed Rhizomys more bamboo
Will H requires a minimum of how many small pieces of bamboo bamboo rats are ready for it?

Entry

A first line integer N, the number of bamboo rat, where 0 <N≤50000;
the number N of the second row represent different Rhizomys weight, separated by spaces, the number of not more than 50,000.

Export

Outputs a number representing the number of bamboo requires a minimum of preparation

Sample input Copy

3
1 2 2

Sample output Copy

4

prompt

1. Sample 1 explained:
a total of three Rhizomys, their weight 122 are
then fed requires a minimum number of bamboo is 1 + 2 + 1 = 4

2. Data range
for data of 5%, 0 <N≤10;
for 30% of the data, 0 <N≤2000;
to 100% of the data, 0 <N≤50000.
Resolution: The title to be traversed twice pros and cons
I am originally intended to define a t, if the latter is larger than the preceding one by one, let t ++, if a little later than the preceding one put into t 1, sum + = t; however, for example, if the sequence: (12 321 )wrong,
So to facilitate positive and negative sides;
AC Code:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e6+10;
int a[maxn];
int b[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(intI = . 1 ; I <= n-; I ++ ) { 
        B [I] = . 1 ; 
    } 
    for ( int I = 2 ; I <= n-; I ++ ) {
         IF (A [I]> A [I- . 1 ]) { // n with the first side of a convenience than a large front 
            B [I] = B [I- . 1 ] + . 1 ; 
        } 
    } 
    
    for ( int I = N- . 1 ; I> = . 1 ; i-- ) {
         IF (a [I]> a [I + . 1 ] && B [I] <= B [I + . 1 ]) { // front backwards facilitate a large ratio after a 
            b [i] = b [i +1]+1;
        }
    }
    
    ll sum=0;
    
    for(int i=1;i<=n;i++){
        sum+=b[i];
    }
    printf("%lld\n",sum);
    return 0;
}

 


Guess you like

Origin www.cnblogs.com/lipu123/p/12169503.html
Hey
Recommended