Kobolds and Catacombs 思维,模拟,前缀,后缀(沈阳)

在这里插入图片描述
题意 :

  • 给一序列,将其分为若干段,要求每一小段内排序后整体非递减,求最多能分为多少段

思路 :

  • 对一段内部,必然有最大值和最小值,要让分的段数最多,只要让下一段的最小值大于这段的最大值,就可以多分一段
  • 所以直接找每个数能接受的最大值和最小值
  • 即找这个数往前的最大值,下个数往后的最小值(找时注意设立两个哨兵)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
using namespace std;
typedef long long ll;

const int N = 1e6 + 10;

int n;
int a[N];
int maxx[N], minn[N];

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int _ = 1;
//    cin >> _;

    while (_ -- )
    {
        cin >> n;
        for (int i = 1; i <= n; i ++ ) cin >> a[i];
        
        maxx[0] = 0;
        minn[n + 1] = 1e9 + 7;
        
        for (int i = 1; i <= n; i ++ )
            maxx[i] = max(maxx[i - 1], a[i]);
        for (int i = n; i >= 1; i -- )
            minn[i] = min(minn[i + 1], a[i]);
        
        int ans = 0;
        for (int i = 1; i <= n; i ++ )
            if (maxx[i] <= minn[i + 1])
                ans ++ ;
        
        cout << ans << endl;
    }
    
    return 0;
}

  • 附上另一种方案,只记录后缀的,且不增加哨兵
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
using namespace std;
typedef long long ll;

const int N = 1e6 + 10;

int n;
int a[N];
int maxx[N], minn[N];

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int _ = 1;
//    cin >> _;

    while (_ -- )
    {
        cin >> n;
        for (int i = 1; i <= n; i ++ ) cin >> a[i];
        
        int mi = a[n];
        for (int i = n; i >= 1; i -- )
        {
            mi = min(mi, a[i]);
            minn[i] = mi;
        }
        
        int mx = 0, ans = 0;
        for (int i = 1; i <= n; i ++ )
        {
            if (mx <= minn[i]) ans ++ ;
            mx = max(mx, a[i]);
        }
        
        cout << ans << endl;
    }
    
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值