POJ1769_Minimizing maximizer_DP|線段樹最佳化

來源:互聯網
上載者:User

Minimizing maximizer

Time Limit: 5000MS   Memory Limit: 30000K
Total Submissions: 4355   Accepted: 1792

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.





#include<cstdio>#include<iostream>#include<cstring>using namespace std;const int maxn = 50000 + 1000;const int inf  = 0x3f3f3f3f;int Seg[maxn * 4];//線段樹維護區間最小值int n, m;//線段樹查詢區間最小值int Query(int p, int l, int r, int x, int y){if(l>= x && r <= y) return Seg[p];int mid = (l + r) >> 1;int res = inf;if(x <= mid) res = Query(p<<1, l, mid, x, y);if(y > mid) res = min(res, Query(p<<1|1, mid+1, r, x, y));return res;}//線段樹單點更新void Update(int p, int l, int r, int x, int y){if(l == r){Seg[p] = min(y, Seg[p]);return;}int mid = (l + r) >> 1;if(x <= mid) Update(p<<1, l, mid, x, y);else Update(p<<1|1, mid+1, r, x, y);Seg[p] = min(Seg[p<<1], Seg[p<<1|1]);}int main(){//線段樹初始化為無窮大memset(Seg, inf, sizeof(Seg));scanf("%d %d", &n, &m);// dp[1] 初始化為 0Update(1, 1, n, 1, 0);while(m--){int s, t;scanf("%d %d", &s, &t);//狀態轉移int temp = Query(1, 1, n, s, t) + 1;Update(1, 1, n, t, temp);}printf("%d\n", Query(1, 1, n, n, n));return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.