Minimizing maximizer
| Time Limit: 5000MS | Memory Limit: 30000K | |
| Total Submissions: 4669 | Accepted: 1909 |
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.
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.
题解:
dp[i]表示把最大的数移到i位置的最小序列,
则dp[en[i]]=min(dp[en[i]],dp[k]+1){st[i]<=k<=en[i]}
由于st[i]~en[i]范围可能较大,因此可以加一个线段树优化一下。
代码:
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=500005;
#define inf 0xf3f3f3f
int dp[maxn];
int st[maxn],en[maxn];
struct node
{
int l,r,m;
int ma;
}tree[maxn<<2];
void build(int l,int r,int rt)
{
tree[rt].l=l;tree[rt].r=r;tree[rt].m=(l+r)>>1;
if(l==r)
{
tree[rt].ma=dp[l];
return;
}
build(l,tree[rt].m,rt<<1);
build(tree[rt].m+1,r,rt<<1|1);
tree[rt].ma=min(tree[rt<<1].ma,tree[rt<<1|1].ma);
}
void update(int x,int L,int rt)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].ma=L;
return;
}
if(tree[rt].m>=x)update(x,L,rt<<1);
else update(x,L,rt<<1|1);
tree[rt].ma=min(tree[rt<<1].ma,tree[rt<<1|1].ma);
}
int query(int l,int r,int rt)
{
if(tree[rt].l>=l&&tree[rt].r<=r)
return tree[rt].ma;
int ans=1e9;
if(l<=tree[rt].m)ans=min(ans,query(l,r,rt<<1));
if(r>tree[rt].m)ans=min(ans,query(l,r,rt<<1|1));
return ans;
}
int main()
{
int n,m;scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d%d",&st[i],&en[i]);
memset(dp,inf,sizeof(dp));
dp[1]=0;
build(1,n,1);
for(int i=0;i<m;i++)
{
dp[en[i]]=min(dp[en[i]],query(st[i],en[i],1)+1);//查询区间最小值
update(en[i],dp[en[i]],1);//更新
}
printf("%d\n",dp[n]);
return 0;
}

本文介绍了一种名为Maximizer的硬件排序系统,该系统通过一系列排序器找出输入中的最大值。文章提出了一种优化方案,旨在减少排序器的数量,同时保持正确结果。使用动态规划与线段树来实现优化,并提供了完整的代码示例。
537

被折叠的 条评论
为什么被折叠?



