POJ 1769 Minimizing maximizer (dp + 线段树)

本文介绍了一种名为Maximizer的新型排序硬件,旨在通过优化排序器管道来减少不必要的排序步骤,从而实现更高效的输入值最大化的查找过程。文章提供了一个算法实例,展示了如何通过特定的数据结构来快速找到最优的排序器子序列。

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

题意:

找最少的区间使得依次连续覆盖所有n 个数。

思路:

令dp[i]表示覆盖到 以i 为终点的区间的最少个数。

那么dp[i] 转移肯定来自  s[j]~t[j] 里面的,我们需要 找一个k 使得 s[j] <= k <= t[j] 中  dp[k]最小值。

dp[i] = min(dp[i], dp[k]+1);

找最小值可以用线段树优化。

边界是dp[1] = 0

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m;

int dp[50000 + 7];
int MIN[50000+7<<2];

const int inf = 0x3f3f3f3f;



void build(int l,int r,int o){
    if (l == r){
        MIN[o] = dp[l];
        return;
    }
    int m = l+r>>1;
    build(l,m,o<<1);
    build(m+1,r,o<<1|1);
    MIN[o] = min(MIN[o<<1], MIN[o<<1|1]);
}

int query(int L,int R,int l,int r,int o){
    if (L <= l && r <= R){
        return MIN[o];
    }

    int m = l+r>>1;
    int ans = inf;
    if (m >= L) ans = min(ans,query(L,R,l,m,o<<1));
    if (m < R) ans = min(ans,query(L,R,m+1,r,o<<1|1));
    return ans;
}


void update(int p,int v,int l,int r,int o){
    if (l == r){
        MIN[o] = v;
        return;
    }
    int m = l+r>>1;
    if (p <= m)update(p,v,l,m,o<<1);
    else update(p,v,m+1,r,o<<1|1);
    MIN[o] = min(MIN[o<<1],MIN[o<<1|1]);
}

int main(){
    scanf("%d %d",&n, &m);
    memset(dp,inf,sizeof dp);
    dp[1] = 0;
    build(1,n,1);
    for (int i = 0; i < m; ++i){
        int u,v;
        scanf("%d %d",&u, &v);
        int qq = query(u,v,1,n,1);
        if (qq+1 < dp[v]){
            dp[v] = qq+1;
            update(v,dp[v],1,n,1);
        }
    }
    printf("%d\n",dp[n]);
    return 0;
}

/**
40 6
20 30
1 10
10 20
20 30
15 25
30 40

ans = 4;

**/



Minimizing maximizer
Time Limit: 5000MS Memory Limit: 30000K
Total Submissions: 4282 Accepted: 1756

Description

The company Chris Ltd. is preparing a new sorting hardware called Maximizer. Maximizer has n inputs numbered from 1 to n. Each input represents one integer. Maximizer has one output which represents the maximum value present on Maximizer's inputs. 

Maximizer is implemented as a pipeline of sorters Sorter(i1, j1), ... , Sorter(ik, jk). Each sorter has n inputs and n outputs. Sorter(i, j) sorts values on inputs i, i+1,... , j in non-decreasing order and lets the other inputs pass through unchanged. The n-th output of the last sorter is the output of the Maximizer. 

An intern (a former ACM contestant) observed that some sorters could be excluded from the pipeline and Maximizer would still produce the correct result. What is the length of the shortest subsequence of the given sequence of sorters in the pipeline still producing correct results for all possible combinations of input values? 

Task 
Write a program that: 

reads a description of a Maximizer, i.e. the initial sequence of sorters in the pipeline, 
computes the length of the shortest subsequence of the initial sequence of sorters still producing correct results for all possible input data, 
writes the result. 

Input

The first line of the input contains two integers n and m (2 <= n <= 50000, 1 <= m <= 500000) separated by a single space. Integer n is the number of inputs and integer m is the number of sorters in the pipeline. The initial sequence of sorters is described in the next m lines. The k-th of these lines contains the parameters of the k-th sorter: two integers ik and jk (1 <= ik < jk <= n) separated by a single space.

Output

The output consists of only one line containing an integer equal to the length of the shortest subsequence of the initial sequence of sorters still producing correct results for all possible data.

Sample Input

40 6
20 30
1 10
10 20
20 30
15 25
30 40

Sample Output

4

Hint

Huge input data, scanf is recommended.

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值