Description
A forest is full of sloths, they are so eager for tree leaves and as a result, very angry.
We assume that the forest is a map of N * M grids, and each of the gird is an empty land or contains a big sloth. It’s guaranteed that the sequence of the sloth is a continuous segment from the leftmost column for every row. ( You may consider that the number of columns M is infinite ).
As a sloth lover, you want to feed all the sloths as quick as possible. Every second you may select a rectangle area of the grids which only contains sloths and feed all the sloths there.
What’s the minimum time required to meet all the sloths’ needs in the forest?
Input
First line of each case contains one numbers N.(1≤ n ≤ 1 000).
The following line contains N numbers Ai, each describing the length of continuous sloths sequence from the left most column in every row. ( 1 <= Ai <= 10^9 )
Output
Output the answer on a single line for each case.
Sample Input
4
3 4 5 5
Sample Output
3
Hint
The distributing situation of the sloths in the sample is as follow:
SSS
SSSS
SSSSS
SSSSS
And you can choose three rectangles to cover it.
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n,a[1003];
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
bool s[1003]={0};
int ans=0;
for(int i=0;i<n;i++){
if(s[i]==0) {
for(int j=i+1;j<n;j++){
if(a[j]<a[i]) {break;}
if(a[j]==a[i]) s[j]=1;
}
ans++;
}
}
printf("%d\n",ans);
}
}