poj1769 Minimizing maximizer线段树优化dp

Minimizing maximizer
Time Limit: 5000MS Memory Limit: 30000K
Total Submissions: 4099 Accepted: 1661

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

Central Europe 2003

英文题目先来翻译一下,直接截图了。。。(挑战程序设计竞赛)


题目抽象出数学模型就是说,最少需要多少个区间能完全覆盖区间[1,n]。

定义dp[i]为覆盖区间[1,i]所需要的最少区间的个数,有状态转移方程,dp[t[i]]=min(dp[t[i],min(dp[t[j]|s[i]<=j<=t[i])+1)。

对于每个i只需更新一个值就行了,然而求最小值最坏情况下时间复杂度可能为O(n),因此时间复杂度为O(mn),并不高效,但是求最小值可以用线段树去优化,时间复杂度为O(logn),就可以在O(mlogn)时间内求解。

提醒自己,一定要注意下标,还有数组大小(否则可能会Runtime Error)。

AC代码如下:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=50005;
const int maxm=500005;
#define INF 0x7ffffff
#define min(a,b) (a<b)?a:b
int n,m;
int s[maxm],t[maxm],data[1<<18-1];
int dp[maxn];
void init(int n_)
{
    //把元素的个数扩大到2的幂
    n=1;
    while(n<n_){
        n=n<<1;
    }
    //把所有的值都设为INF
    for(int i=0;i<2*n-1;i++){
        data[i]=INF;
    }
}
//把第k个值更新为a
void update(int k,int a)
{
    //叶子结点
    k+=n-1;
    data[k]=a;
    //向上更新
    while(k>0){
        k=(k-1)/2;
        data[k]=min(data[2*k+1],data[2*k+2]);
    }
}
//求[a,b)的最小值
//后面的参数是为了计算方便而加入进来的
//k是结点的编号,l、r表示这个结点的对应区间
//在外部调用时,用query(a,b,0,0,n)
int query(int a,int b,int k,int l,int r)
{
    //如果[a,b)和[l,r)不相交,则返回INT_MAX
    if(r<=a||b<=l) return INF;
    //如果[a,b)完全包含[l,r),则返回当前结点值
    if(a<=l&&r<=b) return data[k];
    else{
        //否则返回两个儿子中值较小的一个
        int vl=query(a,b,2*k+1,l,(l+r)/2);
        int vr=query(a,b,2*k+2,(l+r)/2,r);
        return min(vl,vr);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        scanf("%d%d",&s[i],&t[i]);
    }
    int x=n;
    init(n);
    fill(dp,dp+x+1,INF);
    dp[1]=0;
    update(0,0);//注意下标
    for(int i=0;i<m;i++){
        int v=min(dp[t[i]],query(s[i]-1,t[i],0,0,n)+1);//注意下标
        dp[t[i]]=v;
        update(t[i]-1,v);//注意下标
    }
    printf("%d\n",dp[x]);
    return 0;
}
《挑战程序设计竞赛》书中给的解题思路并没有看懂╮(╯_╰)╭

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值