poj 1769 Minimizing maximizer 线段树

本文介绍如何使用线段树解决一个特定的排序器问题,通过单点更新和区间求和来找到使得序列中最大值移动到末尾所需的最少排序器数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

题目链接:http://poj.org/problem?id=1769

题目来源:《挑战》例题。

简要题意:排序器将某区间的排序其他不变,排序器序列最后一个输出的最后一个就是最终结果,问至少要多少排序器最终一定能得到最大值。

题解

整个东西就是线段树的单点更新,区间求和。

维护一个线段树,保存和询问区间的最小值。

对于当前的区间,区间内的最小值+1就是最大值移到右端点的最少次数。

将最少次数更新到线段树中,循环往复。

由于要考虑最坏的情况,因而令1的位置为0,然后最后就是去query n的位置。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 5e4+5;
const int INF = 0x3f3f3f3f;

struct SegmentTree {
    #define lson (rt<<1)
    #define rson ((rt<<1)|1)
    #define MID ((L+R)>>1)
    #define lsonPara lson, L, MID
    #define rsonPara rson, MID+1, R

    const static int TN = N << 2;

    int t[TN];

    void pushUp(int rt) {
        t[rt] = min(t[lson], t[rson]);
    }

    void build(int rt, int L, int R) {
        if (L == R) {
            t[rt] = INF;
        } else {
            build(lsonPara);
            build(rsonPara);
            pushUp(rt);
        }
    }

    void modify(int rt, int L, int R, int x, int v) {
        if (x < L || x > R) return;
        if (L == R) {
            t[rt] = min(t[rt], v);
        } else {
            modify(lsonPara, x, v);
            modify(rsonPara, x, v);
            pushUp(rt);
        }
    }

    int query(int rt, int L, int R, int l, int r) {
        if (l > R || r < L || l > r) return INF;
        if (l <= L && r >= R) return t[rt];
        return min(query(lsonPara, l, r), query(rsonPara, l, r));
    }
};

SegmentTree st;
int main() {
    int n, m, l, r;
    while (scanf("%d%d", &n, &m) == 2) {
        st.build(1, 1, n);
        st.modify(1, 1, n, 1, 0);
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &l, &r);
            int ans = st.query(1, 1, n, l, r);
            st.modify(1, 1, n, r, ans + 1);
        }
        printf("%d\n", st.query(1, 1, n, n, n));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值