POJ 1769 Minimizing maximizer(最少区间覆盖dp + 线段树优化查询区间最小值)

Minimizing maximizer
Time Limit: 5000MS Memory Limit: 30000K
Total Submissions: 4251 Accepted: 1744

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.


题意:给定n和m个有序区间[si,ti],从左端开始覆盖[1,n],问最少取多少个区间能完全覆盖[1,n]。 注意:m个区间有序,在选取时不能破坏区间顺序。


题解:因为区间有序,贪心不可取。 设dp[i]为在最右端为i时需要的区间最少数量。

 那么初始化:dp[1]=0,dp[i]=INF  状态转移方程为 dp[ti] = min(dp[ti],min{dp[j]|si<=j<=ti})


于是我们能写出下面的O(n*m)的算法:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxm = 500000+10;
const int INF = 1e9+10;
const int maxn = 50000+1;
int dp[maxn];
int s[maxm],t[maxm];
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=1;i<=m;++i)
			scanf("%d%d",&s[i],&t[i]);
		dp[1]=0;
		for(int i=2;i<=n;++i)
			dp[i]=INF;
		for(int i=1;i<=m;++i)
		{
			int cnt=INF;
			for(int j=s[i];j<=t[i];++j)
				dp[t[i]]=min(dp[t[i]],dp[j]+1);
		}
		printf("%d\n",dp[n]);
	}
	return 0;
} 


很明显O(n*m)会TLE,对于在区间j|[si,ti]中查询最小的dp[j],我们可以采用线段树优化查询,这样就能做到O(m*logn)的复杂度。


AC代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxm = 500000+10;
const int INF = 1e9+10;
const int maxn = 50000+10;
int dp[maxn];
int s[maxm],t[maxm];
#define lson i*2,l,m
#define rson i*2+1,m+1,r
int minv[maxn*4];

void PushUp(int i)
{
	minv[i]=min(minv[i*2],minv[i*2+1]);
}

void build(int i,int l,int r)
{
	if(l==r){
		minv[i]=INF;
		return ;
	}
	int m=(r+l)/2;
	build(lson);
	build(rson);
	PushUp(i);
}

int query(int ql,int qr,int i,int l,int r)
{
	if(ql<=l&&qr>=r)
		return minv[i];
	int m=(r+l)/2;
	int min_val=INF;
	if(ql<=m)
		min_val=min(min_val,query(ql,qr,lson));
	if(qr>m)
		min_val=min(min_val,query(ql,qr,rson));
	return min_val; 
}

void update(int pos,int val,int i,int l,int r)
{
	if(l==r){
		minv[i]=min(minv[i],val);
		return ;
	}
	int m=(l+r)/2;
	if(pos<=m)
		update(pos,val,lson);
	else
		update(pos,val,rson);
	PushUp(i);
}

int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=1;i<=m;++i)
			scanf("%d%d",&s[i],&t[i]);
		dp[1]=0;
		for(int i=2;i<=n;++i)
			dp[i]=INF;
		build(1,1,n);
		update(1,0,1,1,n);
		for(int i=1;i<=m;++i)
		{
			int cnt=query(s[i],t[i],1,1,n);
			if(cnt!=INF)
			{
				dp[t[i]]=min(dp[t[i]],cnt+1);
				update(t[i],dp[t[i]],1,1,n);
			}
		}
		printf("%d\n",dp[n]);
	}
	return 0;
} 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值