题意:
找最少的区间使得依次连续覆盖所有n 个数。
思路:
令dp[i]表示覆盖到 以i 为终点的区间的最少个数。
那么dp[i] 转移肯定来自 s[j]~t[j] 里面的,我们需要 找一个k 使得 s[j] <= k <= t[j] 中 dp[k]最小值。
dp[i] = min(dp[i], dp[k]+1);
找最小值可以用线段树优化。
边界是dp[1] = 0
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m;
int dp[50000 + 7];
int MIN[50000+7<<2];
const int inf = 0x3f3f3f3f;
void build(int l,int r,int o){
if (l == r){
MIN[o] = dp[l];
return;
}
int m = l+r>>1;
build(l,m,o<<1);
build(m+1,r,o<<1|1);
MIN[o] = min(MIN[o<<1], MIN[o<<1|1]);
}
int query(int L,int R,int l,int r,int o){
if (L <= l && r <= R){
return MIN[o];
}
int m = l+r>>1;
int ans = inf;
if (m >= L) ans = min(ans,query(L,R,l,m,o<<1));
if (m < R) ans = min(ans,query(L,R,m+1,r,o<<1|1));
return ans;
}
void update(int p,int v,int l,int r,int o){
if (l == r){
MIN[o] = v;
return;
}
int m = l+r>>1;
if (p <= m)update(p,v,l,m,o<<1);
else update(p,v,m+1,r,o<<1|1);
MIN[o] = min(MIN[o<<1],MIN[o<<1|1]);
}
int main(){
scanf("%d %d",&n, &m);
memset(dp,inf,sizeof dp);
dp[1] = 0;
build(1,n,1);
for (int i = 0; i < m; ++i){
int u,v;
scanf("%d %d",&u, &v);
int qq = query(u,v,1,n,1);
if (qq+1 < dp[v]){
dp[v] = qq+1;
update(v,dp[v],1,n,1);
}
}
printf("%d\n",dp[n]);
return 0;
}
/**
40 6
20 30
1 10
10 20
20 30
15 25
30 40
ans = 4;
**/
Minimizing maximizer 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. 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. Source Central Europe 2003 |
Time Limit: 5000MS | | Memory Limit: 30000K |
Total Submissions: 4282 | | Accepted: 1756 |
[Submit] [Go Back] [Status] [Discuss]
Home Page
Go Back
To top
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator