poj1769(Minimizing maximizer)dp+线段树

 

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

 

 

 

 

题意:从m个区间中选取最少的区间覆盖[1, n],覆盖顺序和给定的顺序相同。

 

题解:如果没有后半句话,那么这题很简单,排序然后贪心选取就行。现在给定了顺序,就不能这么做了。

用动态规划,设第i个区间为[si, ti],dp[i][j]表示处理到第i个区间且覆盖区间最右端为j需要的最少区间数。

ti != j时,dp[i][j] = dp[i-1][j];ti = j时,dp[i][j] = min(dp[i-1][j], min{dp[i-1][j'] | si <= j' <= ti} + 1)。

这样复杂度为O(mn),太高,想其他办法吧。由于i是有i-1得到的,所以可以减少一维。对于每一个i,

dp[ti] = min(dp[ti], min{dp[j'] | si <= j' <= ti} + 1)

貌似这样复杂度降到了O(m),但是最坏情况下仍然可能达到O(mn),这时可以使用线段树来维护区间最小值,这样复杂度就降到了O(mlogn)

第一次独立写线段树,以前都是抄模板的。。。

 

 

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <numeric>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 50010;

int dp[maxn];
int st[1<<18];
int n, m;

void build(int k, int l, int r)
{
    st[k] = INF;
    if (r - l == 1)
        return;
    build(k*2+1, l, (l+r)>>1);
    build(k*2+2, (l+r)>>1, r);
}

void update(int s, int x, int k, int l, int r)
{
    if (r - l > 1)
    {
        int m = (l + r) >> 1;
        if (s < m)
            update(s, x, k*2+1, l, m);
        else
            update(s, x, k*2+2, m, r);
    }
    st[k] = min(st[k], x);
}

int query(int a, int b, int k, int l, int r)
{
    if (a <= l && b >= r)
        return st[k];
    if (b <= l || a >= r)
        return INF;
    int vl = query(a, b, k*2+1, l, (l+r)>>1);
    int vr = query(a, b, k*2+2, (l+r)>>1, r);
    return min(vl, vr);
}

int main()
{
    cin >> n >> m;
    build(0, 0, n);
    fill(dp, dp+n, INF);
    dp[0] = 0;
    update(0, 0, 0, 0, n);
    while (m--)
    {
        int s, t;
        scanf("%d%d", &s, &t);
        int v = min(dp[t-1], query(s-1, t, 0, 0, n)+1);
        dp[t-1] = v;
        update(t-1, v, 0, 0, n);
    }
    printf("%d\n", dp[n-1]);
    return 0;
}

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法码上来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值