• 原题如下:

    Minimizing maximizer
    Time Limit: 5000MS   Memory Limit: 30000K
    Total Submissions: 5104   Accepted: 2066

    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.
  • 题解:首先,考虑在什么情况下可以正常工作,假设输入的第i个数是应该输出的最大值,此时在第一个满足sk≤i≤tk的Sorter的输出中,这个值被移动到了第tk个位置,接下去,在一个满足sk'≤tk≤tk'且k'>k的Sorter的输出中,这个值又被移动到了第tk'个。不断重复这样的操作,如果最后可以被移动到第n个,那么就表示Maximizer可以正常工作(实际上,就是要按照线段的输入顺序,将[1, n]从左到右依次覆盖,求所需线段个数的最小值)。因此只要i=1的情况可以正常工作,那么对于任意的i都可以正常工作。不妨假设第一个数是应该输出的最大值,考虑DP:
      dp[i][j]:=到第i个Sorter为止,最大值被移动到第j个位置所需要的最短的子序列的长度(INF表示不存在这样的序列)
      dp[0][1]=0
      dp[0][j]=INF(j>1)
      dp[i+1][j]=①dp[i][j] (ti ≠ j)
                      ②min( dp[i][j] , min{dp[i][j']|si≤j'≤ti}+1 ) (t= j)
    由于这个DP的复杂度是O(nm)的,仍然无法在规定的时间内求出答案。但是对于(ti ≠ j)时有dp[i+1][j]=dp[i][j],我们可以使用同一个数组不断对自己更新:
      dp[j]:=最大值被移动到第j个位置所需要的最短的子序列的长度。(INF表示不存在这样的序列)
    初始化:
      dp[1]=0
      dp[j]=INF(j>1)
    对于每个i,这样更新:
      dp[ti]=min(  dp[ti], min{dp[j']|si≤j'≤ti}+1 )
    这样,对于每个i都只需更新一个值就可以了,但求最小值时,最坏情况下要O(n)的时间,最后复杂度还是O(nm)。如果使用线段树来维护,就可以在O(mlogn)时间内求解了。
  • 代码:
     #include <cstdio>
    #include <cctype>
    #include <algorithm>
    #define number s-'0' using namespace std; const int INF=0x3f3f3f3f;
    const int MAX_N=;
    const int MAX_M=;
    int n,m;
    int s[MAX_M], t[MAX_M];
    int dp[MAX_N+];
    int dat[*MAX_N]; void read(int &x)
    {
    char s;
    bool flag=;
    x=;
    while (!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for (x=number; isdigit(s=getchar());x=x*+number);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if (x<)
    {
    putchar('-');
    x=-x;
    }
    if (x>) write(x/);
    putchar(x%+'');
    } void rmq_init(int k, int l, int r);
    void update(int u, int v, int k, int l, int r);
    int query(int a, int b, int k, int l, int r); int min(int x, int y)
    {
    if (x<y) return x;
    return y;
    } int main(int argc, char * argv[])
    {
    read(n);read(m);
    for (int i=; i<m; i++)
    {
    read(s[i]);read(t[i]);
    s[i]--;t[i]--;
    }
    rmq_init(, , n);
    fill(dp, dp+MAX_N, INF);
    dp[]=;
    update(, , , , n);
    for (int i=; i<m; i++)
    {
    int v=min(dp[t[i]], query(s[i], t[i]+, , , n)+);
    dp[t[i]]=v;
    update(t[i], v, , , n);
    }
    write(dp[n-]);putchar('\n');
    } void rmq_init(int k, int l, int r)
    {
    dat[k]=INF;
    if (r-l==) return;
    rmq_init(k*+, l, (l+r)/);
    rmq_init(k*+, (l+r)/, r);
    } void update(int u, int v, int k, int l, int r)
    {
    if (r-l==)
    {
    dat[k]=v;
    return;
    }
    else
    {
    int m=(l+r)/;
    if (u<m) update(u, v, k*+, l, m);
    else update(u, v, k*+, m, r);
    dat[k]=min(dat[k*+], dat[k*+]);
    }
    } int query(int a, int b, int k, int l, int r)
    {
    if (r<=a || b<=l) return INF;
    if (a<=l && r<=b) return dat[k];
    else
    {
    int m=(l+r)/;
    int vl=query(a, b, k*+, l, m);
    int vr=query(a, b, k*+, m, r);
    return min(vl, vr);
    }
    }

Minimizing maximizer(POJ 1769)的更多相关文章

  1. POJ 1769 Minimizing maximizer(DP+zkw线段树)

    [题目链接] http://poj.org/problem?id=1769 [题目大意] 给出一些排序器,能够将区间li到ri进行排序,排序器按一定顺序摆放 问在排序器顺序不变的情况下,一定能够将最大 ...

  2. poj 1769 Minimizing maximizer 线段树维护dp

    题目链接 给出m个区间, 按区间给出的顺序, 求出覆盖$ [1, n] $ 至少需要多少个区间. 如果先给出[10, 20], 在给出[1, 10], 那么相当于[10, 20]这一段没有被覆盖. 令 ...

  3. POJ.1769.Minimizing maximizer(线段树 DP)

    题目链接 /* 题意:有m个区间,问最少要多少个区间能覆盖[1,n] 注:区间要按原区间的顺序,不能用排序贪心做 设dp[i]表示最右端端点为i时的最小值 dp[e[i]]=min{dp[s[i]]~ ...

  4. POJ 1769 Minimizing maximizer (线段树优化dp)

    dp[i = 前i中sorter][j = 将min移动到j位置] = 最短的sorter序列. 对于sorteri只会更新它右边端点r的位置,因此可以把数组改成一维的,dp[r] = min(dp[ ...

  5. POJ1769 Minimizing maximizer(DP + 线段树)

    题目大概就是要,给一个由若干区间[Si,Ti]组成的序列,求最小长度的子序列,使这个子序列覆盖1到n这n个点. dp[i]表示从第0个到第i个区间且使用第i个区间,覆盖1到Ti所需的最少长度 对于Si ...

  6. uva 1322 Minimizing Maximizer

    题意: 有n个数,m个排序器,每个排序器可以把区间ai到bi的数从小到大排序.这m个排序器的输出就是m个排序之后的第n个数. 现在发现有些排序器是多余的.问至少需要多少个排序器可以使得输出不变.排序器 ...

  7. UVA-1322 Minimizing Maximizer (DP+线段树优化)

    题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...

  8. poj1769 Minimizing maximizer

    传送门 题目大意 给你m个机器,n个数,每个机器可以给n个数的某一段排序,求最少使用几个机器,保证可以把这个n个数排好序 分析 我们可以想到dpij表示考虑前i个机器让最大的数到达点j至少需要使用多少 ...

  9. Minimizing Maximizer

    题意: 最少需要多少个区间能完全覆盖整个区间[1,n] 分析: dp[i]表示覆盖[1,i]最少需要的区间数,对于区间[a,b],dp[b]=min(dp[a...b-1])+1;用线段树来维护区间最 ...

随机推荐

  1. JavaScript 回调函数的简单示例

    回调函数 回调函数也被称为高阶函数 所谓高阶函数,就是说值 函数作为参数被传递或者返回值输出 操作函数的函数称为 高阶函数 把一段可执行的代码(一个函数)作为参数传递给其他的代码(另一个函数),并在需 ...

  2. 使用BERT进行情感分类预测及代码实例

    文章目录 0. BERT介绍 1. BERT配置 1.1. clone BERT 代码 1.2. 数据处理 1.2.1预训练模型 1.2.2数据集 训练集 测试集 开发集 2. 修改代码 2.1 加入 ...

  3. POJ2806 Square

    题目描述 给定\(2*1\)和\(2 * 2\)两种规格的地砖,请问\(2 * n\)的地面总共有多少种方法? 下面是铺满\(2*17\)的地面的示意图. 输入输出格式 输入 多组数据,每组数据包括1 ...

  4. 【MySQL】记一次线上重大事故:二狗子竟然把线上数据库删了!!

    写在前面 估计二狗子这几天是大姨夫来了,心情很郁闷,情绪也很低落,工作的时候也有点心不在焉.让他发个版本,结果,一行命令下去把线上的数据库删了!你没听错:是删掉了线上的数据库!运营那边顿时炸了锅:怎么 ...

  5. XSS的构造技巧

    XSS的构造技巧 By:Mirror王宇阳 E-mail:2821319009@qq.com 本文为长期持续维护,一旦遇到新的技术技巧将及时更新本文内容 非常的欢迎技术大佬纠正并补充,也欢迎技术同僚讨 ...

  6. neutron plugin 笔记

    neutron-plugin 分为 core-plugin 和 service-plugin 两类 L2-L3称为core plugin,包含network.subnet.portL4-L7称为ser ...

  7. Hibernate在MySQL中查询区分大小写

    MySQL查询中默认是不区分大小写的,比如如下语句: SELECT * from PersonBehDevice where flag=0 AND devicecode ='ddjc' 查询结果如下: ...

  8. python - 常用数据清洗方法-重复项处理

    在数据的处理过程中,一般都需要进行数据清洗工作,如数据集是否存在重复,是否存在缺失,数据是否具有完整性和一致性,数据中是否存在异常值等.发现诸如此类的问题都需要针对性地处理,下面我们一起学习常用的数据 ...

  9. 区块链入门到实战(29)之Solidity – 环境搭建

    在线开发环境Remix(推荐) 学习Solidity推荐使用在线开发环境Remix,本教程的例子将使用Remix开发运行. 安装本地编译器 安装 nodejs / npm node官方网站下载node ...

  10. java23种设计模式—— 一、设计模式介绍

    Java23种设计模式全解析 目录 java23种设计模式-- 一.设计模式介绍 java23种设计模式-- 二.单例模式 java23种设计模式--三.工厂模式 java23种设计模式--四.原型模 ...