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

238人阅读 评论(0)

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;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：158736次
• 积分：6564
• 等级：
• 排名：第3619名
• 原创：522篇
• 转载：3篇
• 译文：0篇
• 评论：12条
文章分类
阅读排行
最新评论