Minimizing Maximizer

题意:

最少需要多少个区间能完全覆盖整个区间[1,n]

分析:

dp[i]表示覆盖[1,i]最少需要的区间数,对于区间[a,b],dp[b]=min(dp[a...b-1])+1;用线段树来维护区间最小值。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define N 50010
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
struct seg{
    int s,e;
}g[N*10];
int minv[N*4],n,q;
void pushup(int rt){
    minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
        if(l==1)minv[rt]=0;
        else minv[rt]=INF;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p,int l,int r,int rt,int v){
    if(l==r){
        minv[rt]=min(minv[rt],v);
        return;
    }
    int m=(l+r)>>1;
    if(p<=m)update(p,lson,v);
    else update(p,rson,v);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(l>=L&&r<=R)
        return minv[rt];
    int tmp=INF;
    int m=(l+r)>>1;
    if(L<=m)tmp=min(tmp,query(L,R,lson));
    if(R>m)tmp=min(tmp,query(L,R,rson));
    return tmp;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&q);
        for(int i=0;i<q;++i)
        scanf("%d%d",&g[i].s,&g[i].e);
        build(1,n,1);
        for(int i=0;i<q;++i){
            int ans=min(query(g[i].s,g[i].e-1,1,n,1)+1,query(g[i].e,g[i].e,1,n,1));
                update(g[i].e,1,n,1,ans);
        }
        printf("%d\n",query(n,n,1,n,1));
        if(t)printf("\n");
    }
return 0;
}
时间: 2024-08-25 17:30:50

Minimizing Maximizer的相关文章

poj1769 Minimizing maximizer

poj1769 Minimizing maximizer 总之,存代码 = = #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 500000+10; const int MAXM = 500000+10; const int INF

POJ 1769 Minimizing maximizer(DP+zkw线段树)

[题目链接] http://poj.org/problem?id=1769 [题目大意] 给出一些排序器,能够将区间li到ri进行排序,排序器按一定顺序摆放 问在排序器顺序不变的情况下,一定能够将最大值交换到最后一位至少需要保留几个排序器 [题解] 我们发现,对于每个排序器,dp[ri]=min(dp[ri],min(dp[li]~dp[ri-1])+1) 我们用线段树对dp值进行最小值维护,顺序更新即可. [代码] #include <cstdio> #include <algorit

poj 1769 Minimizing maximizer(dp+线段树)

题意:数列长度为n,m次操作(n<=50000,m<=500000),每次操作将区间[si,ti]从小到大排序,求至少使用几次操作使数列的最后一个数与经过所有操作后相等: 思路:选取最少的操作得到最优解,一般采用dp; 假设原数列的第1个数为最大值,dp[j]表示最大值移动到第j个位置需要至少的操作数: 初始令dp[1]=0,dp[j]=inf(j>1); 对于每个i,有dp[ti]=min(dp[ti],min(dp[j](si<=j<=ti))+1); 若复杂度为O(nm

POJ 1769 Minimizing maximizer

dp[i = 前i中sorter][j = 将min移动到j位置] = 最短的sorter序列. 对于sorteri只会更新它右边端点r的位置,因此可以把数组改成一维的,dp[r] = min(dp[r],dp[j]+1), l≤j<r. 不是滑窗,单调队列用用不了,但是可以用线段树去维护这个最小值. /********************************************************* * ------------------ * * author Abyssal

POJ 1769 Minimizing maximizer ( 线段树 &amp;&amp; DP )

题意 : 给定一个区间长度 n ,接下来给出 m 个子区间,要求最少选出多少个区间才能使得 1~n 这个区间被所选的所有子区间覆盖 分析 :  暴力枚举所有可能的组合可以达到 O( m^m ) ,完全不行 这里考虑动态规划解法 定义 dp[i][j] : 到第 i 个为止,完全覆盖点(即从 1~这个点都能保证被覆盖)到达第 j 个位置所需的最少子区间 则初始化为 dp[0][2~n] = INF.dp[0][1] = 0 假设当前第 i 个子区间用 ( si,ti ) 表示 则状态转移方程为 (

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

JavaScript Patterns 2.2 - Minimizing Globals

Access a global variable in a browser environment: myglobal = "hello"; // antipattern console.log(myglobal); // "hello" console.log(window.myglobal); // "hello" console.log(window["myglobal"]); // "hello"

The Note of the Paper "Optimal Decoding of Linear Codes for Minimizing Symbol Error Rate"(1)

The paper "Optimal Decoding of Linear Codes for Minimizing Symbol Error Rate", which is the source of the BCJR decoding algorithm, is my current object to understand. The following is the notes of this paper. Some of them are the original words

文档翻译-Minimizing your app&#39;s Memory Footprint

原文地址:https://developer.apple.com/library/ios/technotes/tn2434/_index.html#//apple_ref/doc/uid/DTS40017252这个文档是 指导利用instrument 来分析App内存的,受益匪浅啊!!! 优化App的内存 技术文档TN2434 优化App内存 内存的优化像App的交互性和稳定性一样也是非常重要的.我们都不难发现App在不同的设备和不同系统中所占的内存也是不同的,而我们的设备内存是有限的.减少ap