Minimizing maximizer 线段树+dp

LINK

Minimizing maximizer
Time Limit: 5000 msMemory Limit: 32768 KB

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(i[1], j[1]), …, Sorter(i[k], j[k]). 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 the 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 Specification

The input consists of several test cases.
For each case, the first line contains two integers N and M (2 <= N <= 50 000, 1 <= M <= 500 000) 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 i[k] and j[k] (1 <= i[k] < j[k] <= N) separated by a single space.
Output Specification

For each test case, print in one line 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

Output for the Sample Input

4

  • 题意就是找到覆盖1~n 的最少线段数目 前一条线段应该与后一条线段有相交点
    dp[i]表示到达i这个点所需的最少线段数目
    dp[b]=min(dp[b],dp[i]+1(a<=i<=b,也就是在这个线段之间))
    update实现更新操作
    query 查找min
#include<bits/stdc++.h>
using namespace std;
const int N=50010,inf=0x3f3f3f3f;
int t[N<<2];
int n,m;
void up(int rt)
{
	t[rt]=min(t[rt<<1],t[rt<<1|1]);
} 
void update(int rt,int l,int r,int x,int v)// 更新dp[x]=min(dp[x],v)
{
	if(l==r) 
	{
	t[rt]=min(v,t[rt]);return ;
	}
	int mid=(l+r)>>1;
	if(x<=mid) update(rt<<1,l,mid,x,v);
    else  update(rt<<1|1,mid+1,r,x,v);
	up(rt);
} 
int query(int rt,int l,int r,int L,int R)
{
	if(l>=L&&r<=R) return t[rt];
	int mid=(l+r)>>1;
	int minn=inf;
	if(L<=mid) minn=query(rt<<1,l,mid,L,R);
	if(R>mid ) minn=min(minn,query(rt<<1|1,mid+1,r,L,R));
	return minn; 
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<n<<2;i++) t[i]=inf;
		update(1,1,n,1,0);//dp[1]=0初始化诶
		for(int i=1;i<=m;i++)
		{
			int a,b;scanf("%d%d",&a,&b);
			int o=query(1,1,n,a,b)+1;
			update(1,1,n,b,o);
		}
		printf("%d\n",query(1,1,n,n,n));
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值