Minimizing maximizer
Time Limit: 5000MS | Memory Limit: 30000K | |
Total Submissions: 3773 | Accepted: 1501 |
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.
题意:
Maximizer是一个接受n个数作为输入,并输出他们的最大值的装置。这个装置由m个叫做Sorter的装置依次连接而成。第k个Sorter把第k-1个Sorter的输出作为输入,然后将第Sk到第Tk个值进行排序后,保存其余部分不变输出。Maximizer的输入就是第一个Sorter的输入,最后一个Sorter输出的第n个值就是Maximizer的输出。从组成Maximizer的Sorter中去掉几个之后,Maximizer有可能还可以正常工作。现在给定Sorter的序列,求其中最短的一个子序列(可以不连续)使得Maximizer仍然可以正常工作。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 50005;
const int MAXM = 500005;
const int inf = 0x2f7f7f7f;
int a[MAXN<<2];
void build(int l, int r, int rt) {
a[rt] = inf;
if (l == r) return ;
int m = (l + r) >> 1;
build(l, m, rt<<1);
build(m+1, r, rt<<1|1);
}
void update(int R, int val, int l, int r, int rt) {
if (a[rt] > val) a[rt] = val;
if (l != r) {
int m = (l + r) >> 1;
if (R <= m) update(R, val, l, m, rt<<1);
else update(R, val, m+1, r, rt<<1|1);
}
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R)
return a[rt];
int m = (l + r) >> 1;
if (R <= m) return query(L, R, l, m, rt<<1);
else if (L > m) return query(L, R, m+1, r, rt<<1|1);
else {
int ret1 = query(L, R, l, m, rt<<1);
int ret2 = query(L, R, m+1, r, rt<<1|1);
return (ret1 > ret2) ? ret2 : ret1;
}
}
int main() {
int n, m, a, b;
while (scanf("%d %d", &n, &m) == 2) {
build(1, n, 1);
update(1, 0, 1, n, 1);
for (int i=0; i<m; i++) {
scanf("%d %d", &a, &b);
if (a < b) {
int x = query(a, b-1, 1, n, 1);
update(b, x+1, 1, n, 1);
}
}
printf("%d\n", query(n, n, 1, n, 1));
}
return 0;
}