关闭

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

238人阅读 评论(0) 收藏 举报
分类:

题目链接
题意:给你n个数,然后你把他们分块,然后每块有序后,整体就是有序的。问最多分几块。
解法:官方题解:

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)

也就是我们看一个数x前面的最大值是不是比x后面的最小值小,如果小的话,那么可以划分x。最后统计这样的x总数。先直接计算出,suffMaxm和suffMin数组即可


#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条
    文章分类
    最新评论