#Poj1769#Minimizing maximizer(Dp+线段树优化)

来源:互联网 发布:java调用父类方法 编辑:程序博客网 时间:2024/06/05 10:20

Minimizing maximizer
Time Limit: 5000MS Memory Limit: 30000KTotal Submissions: 4367 Accepted: 1799

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 620 301 1010 2020 3015 2530 40

Sample Output

4

Hint

Huge input data, scanf is recommended.

这题的题意确实是坑得很,起码我光看英文要看好半天,现在给出网上的两种题意,大家自行体会一下。

一:

Maximizer是一个接受n个数作为输入,并输出他们的最大值的装置。这个装置由m个叫做Sorter的装置依次连接而成。第k个Sorter把第k-1个Sorter的输出作为输入,然后将第Sk到第Tk个值进行排序后,保存其余部分不变输出。Maximizer的输入就是第一个Sorter的输入,最后一个Sorter输出的第n个值就是Maximizer的输出。从组成Maximizer的Sorter中去掉几个之后,Maximizer有可能还可以正常工作。现在给定Sorter的序列,求其中最短的一个子序列(可以不连续)使得Maximizer仍然可以正常工作。

二:

按照线段的输入顺序, 将线段[1, n]从左到右依次覆盖, 求最小的覆盖线段总数.

定义Dp[i]表示按照输入顺序覆盖区间[1, i]所需要的线段数量,Dp[i] = Dp[j] + 1,

其中线段j的右端点必须大于等于线段i的右端点(比如当i为[9,12]时,j起码得是[4, 9],就是说线段的右端点其实是开区间)

用线段树维护线段右端点处Dp最小值,线段树的下标是线段端点标号(N并不大,可以不离散化)


Code:

StatusAcceptedTime766msMemory2396kBLength1518LangG++Submitted2017-06-07 17:47:40SharedRemoteRunId17064282

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;const int Maxn = 50005;const int INF = 0x3f3f3f3f;struct node{int l, r, val;}Tr[Maxn << 3];int N, M;int Dp[Maxn];int min(int a, int b){return a < b ? a : b;}int max(int a, int b){return a < b ? b : a;}void getint(int & num){char c;int flg = 1;num = 0;while((c = getchar()) < '0' || c > '9')if(c == '-')flg = -1;while(c >= '0' && c <= '9'){num = num * 10 + c - 48;c = getchar();}num *= flg;}void build_tr(int i, int l, int r){Tr[i].l = l, Tr[i].r = r;Tr[i].val = INF;if(l == r)return ;int mid = (l +r) >> 1;build_tr(i << 1, l, mid);build_tr(i << 1 | 1, mid + 1, r);}void insert(int i, int p, int val){if(p > Tr[i].r || p < Tr[i].l)return ;if(p == Tr[i].l && p == Tr[i].r){Tr[i].val = min(Tr[i].val, val);return ;}insert(i << 1, p, val);insert(i << 1 | 1, p, val);Tr[i].val = min(Tr[i << 1].val, Tr[i << 1 | 1].val);}int Query(int i, int l, int r){if(l > Tr[i].r || r < Tr[i].l)return INF;if(l <= Tr[i].l && r >= Tr[i].r)return Tr[i].val;return min(Query(i << 1, l, r), Query(i << 1 | 1, l, r));}int main(){while(~scanf("%d%d", &N, &M)){memset(Dp, 0x3f, sizeof(Dp));build_tr(1, 0, N);insert(1, 1, 0);int l, r, now;for(int i = 1; i <= M; ++ i){getint(l), getint(r);now = Query(1, l, r) + 1;if(now < Dp[r]){Dp[r] = now;insert(1, r, now);}}printf("%d\n", Dp[N]);}return 0;}


原创粉丝点击