Minimizing maximizer - POJ 1769 dp+线段树
来源:互联网 发布:js获取页面高度赋给div 编辑:程序博客网 时间:2024/06/15 02:47
Minimizing maximizer
Time Limit: 5000MS Memory Limit: 30000KTotal Submissions: 3399 Accepted: 1350
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 620 301 1010 2020 3015 2530 40
Sample Output
4
Hint
Huge input data, scanf is recommended.
题意:给你m条线段,让你按照顺序选择其中的一些线段,使得从左到右覆盖1-n,问至少选多少条线段。
思路:如果没有顺序的话,可以贪心做,但是这里需要用到线段树。dp[k]由dp[l]到dp[k]中最小的+1转移过来。
AC代码如下:
<span style="font-size:18px;">#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{ int l,r,minn;}tree[200010];int INF=1e9;void build(int o,int l,int r){ tree[o].l=l; tree[o].r=r; tree[o].minn=INF; if(l==r) return; int mi=(l+r)/2; build(o<<1,l,mi); build(o<<1|1,mi+1,r);}int query(int o,int l,int r){ if(tree[o].l==l && tree[o].r==r) return tree[o].minn; int mi=(tree[o].l+tree[o].r)/2; if(r<=mi) return query(o<<1,l,r); else if(l>mi) return query(o<<1|1,l,r); else return min(query(o<<1,l,mi),query(o<<1|1,mi+1,r));}void update(int o,int pos,int val){ if(tree[o].l==tree[o].r) { tree[o].minn=min(tree[o].minn,val); return; } int mi=(tree[o].l+tree[o].r)/2; if(pos<=mi) update(o<<1,pos,val); else update(o<<1|1,pos,val); tree[o].minn=min(tree[o<<1].minn,tree[o<<1|1].minn);}int dp[50010];int main(){ int n,m,i,j,k,l,r; scanf("%d%d",&n,&m); for(i=2;i<=n;i++) dp[i]=INF; build(1,1,n); update(1,1,0); for(i=1;i<=m;i++) { scanf("%d%d",&l,&r); k=query(1,l,r)+1; if(dp[r]>k) { dp[r]=k; update(1,r,k); } } printf("%d\n",dp[n]);}</span>
1 0
- poj 1769 Minimizing maximizer(线段树&dp)
- Minimizing maximizer - POJ 1769 dp+线段树
- poj 1769 Minimizing maximizer 线段树优化的dp
- POJ 1769 Minimizing maximizer (dp + 线段树)
- poj 1769 Minimizing maximizer 线段树
- POJ 1769 Minimizing maximizer(最少区间覆盖dp + 线段树优化查询区间最小值)
- poj1769 Minimizing maximizer线段树优化dp
- #Poj1769#Minimizing maximizer(Dp+线段树优化)
- poj 1769 Minimizing maximizer 单点更新线段树
- POJ 1769 Minimizing maximizer
- POJ 1769 Minimizing maximizer
- POJ 1769 Minimizing maximizer
- POJ 1769 Minimizing maximizer
- POJ 1769 Minimizing maximizer 笔记
- zoj2451-Minimizing maximizer(线段树,超时)
- UVALive 2930 Minimizing Maximizer(最小区间覆盖数 DP + 线段树优化)
- Minimizing Maximizer UVA
- POJ 1769_Minimizing maximizer
- 循环: while循环、do..while 循环、for循环
- <NoSQL精粹>---sadalage/fowler
- Eclipse中属性编辑器插件的在线安装
- UVALive 3029 City Game 悬线法求最大子矩阵面积 dp
- google maps API 替换更换为 baidu maps API
- Minimizing maximizer - POJ 1769 dp+线段树
- hdu-1114 Piggy-Bank
- Backgroud-Master Data-Sales Org.
- Codeforces Round #287 (Div. 2)A(排序)
- Eclispe中的Junit3快速起步
- Codeforces GYM 100548 F - Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest
- poj 3045 Cow Acrobats 贪心
- 1001Jump and Jump...
- LeetCode 3Sum Closest