# Codeforces Round #332 (Div. 2)C. Day at the Beach（好题，）

Let’s take a minute to see how the best answer should look like. Let
Hi be a sorted sequence of hi. Let E — set of indices of the last
elements of each block. Then e E, first e sorted elements of sequence
hi are equal to the first e elements of the sequence Hj. So, it is not
difficult to notice that the size of E is the answer for the problem.
Firstly, we need to calculate two arrays: prefmax and suffmin, where
prefmaxi — maximum between a1, a2, …, ai, and suffmini — minimum
between ai, ai + 1, …, an. If you want to get the answer, just
calculate the number of indices i that prefmaxi  ≤  suffmini + 1.
Time: O(N)


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define X first
#define Y second
#define cl(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
const int maxn=100005;
const LL inf=1LL<<45;
const LL mod=1e9+7;
LL a[maxn],suffMax[maxn],suffMin[maxn];
int main(){
int n;scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=1;i<=n;i++){
suffMax[i]=max(suffMax[i-1],a[i]);
}
suffMin[n+1]=inf;
for(int i=n;i>=1;i--){
suffMin[i]=min(suffMin[i+1],a[i]);
}
suffMin[1]=inf;
LL ans=0;
for(int i=1;i<=n;i++){
if(suffMax[i]<=suffMin[i+1])ans++;
}
printf("%lld\n",ans);
return 0;
}



• 本文已收录于以下专栏：

举报原因： 您举报文章：Codeforces Round #332 (Div. 2)C. Day at the Beach（好题，） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)