[Usaco2013 Open]Photo(dp+单调队列)

题目描述

Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N <= 200,000), which, as always, are conveniently numbered from 1..N. Accordingly, he snapped M (1 <= M <= 100,000) photos, each covering a contiguous range of cows: photo i contains cows a_i through b_i inclusive. The photos collectively may not necessarily cover every single cow. After taking his photos, FJ notices a very interesting phenomenon: each photo he took contains exactly one cow with spots! FJ was aware that he had some number of spotted cows in his herd, but he had never actually counted them. Based on his photos, please determine the maximum possible number of spotted cows that could exist in his herd. Output -1 if there is no possible assignment of spots to cows consistent with FJ"s photographic results. 

给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点

输入

 * Line 1: Two integers N and M.

* Lines 2..M+1: Line i+1 contains a_i and b_i.

输出

* Line 1: The maximum possible number of spotted cows on FJ"s farm, or -1 if there is no possible solution.

样例输入

5 3
1 4
2 5
3 4

 INPUT DETAILS: There are 5 cows and 3 photos. The first photo contains cows 1 through 4, etc.

样例输出

1
刚开始以为会是线段树区间操作之类的,结果是dp...
状态数组f[i]表示第i个点为spotted cow的最大点数,则f[i]=f[j]+1(j合法).
接下来考虑对每个点来说可以转移的合法区间。
设L[i]为覆盖i的所有区间中最小的左端点,R[i]为覆盖i的区间中最大的右端点;
因为每个photo都要覆盖一个spotted cow,所以R[j]为第一个右端点小于L[i]的photo的右端点;
因为每个photo都只能覆盖一个spotted cow,所以R[j]<L[i];
根据这个,我们就可以找到一个合法的转移区间来.
在加上我们的L[i]和R[i]数组都是单调递增的,就可以用单调队列解决问题.
#include <cstdio>
#include <iostream>
#include <algorithm>
#define maxn 200005
#define maxx 100005
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
inline int read()
{   char c=getchar();int x=0,y=1;
    while(c<'0'||c>'9'){if(c=='-') y=-1;c=getchar();}
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*y;
}
inline int m_min(int x,int y){return x<y?x:y;}
inline int m_max(int x,int y){return x>y?x:y;}
int n,m,head,tail,q[maxn],L[maxn],R[maxn],edg,f[maxn];
struct node
{   int x,y;
    node(int a=0,int b=0):x(a),y(b){}
    friend bool operator <(const node& a,const node& b){return a.y<b.y;}
}window[maxx];
void init()
{   int tmp=0x7fffffff;
    for(int i=n,j=m;i>=1;--i)
    {   while(j&&window[j].y>=i) tmp=m_min(tmp,window[j].x),--j;//寻找合法区间的右边界,注意右边的开闭
        R[i]=tmp;//覆盖i的所有区间中最小的左端点.
    }
    tmp=0;
    for(int i=1,j=1;i<=n;++i)
    {   while(j<=m&&window[j].y<i) tmp=m_max(tmp,window[j].x),++j;//寻找合法区间的左边界
        L[i]=tmp;//不覆盖i的区间中最大的左端点
    }
    R[edg]=n+1;
}
void dp()
{   head=1;tail=0;
    for(int i=1,now=0;i<=edg;++i)//第n个可以不选,所以要dp到n+1来考虑n不选的情况.
    {   //cout<<R[i]<<endl;
        for(;now<i&&now<R[i];++now)
        {   if(f[now]!=-1)
            {   while(head<=tail&&f[q[tail]]<f[now]) --tail;
                q[++tail]=now;
            }
        }
        while(head<=tail&&q[head]<L[i]) ++head;
        if(head<=tail) f[i]=f[q[head]]+(i!=edg);
        else f[i]=-1;
    }
}
int main()
{   n=read();m=read();int x,y;edg=n+1;
    for(int i=1;i<=m;++i)
    {   x=read();y=read();
        window[i]=node(x,y);
    }
    sort(window+1,window+m+1);
    init();dp();
    printf("%d",f[edg]);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值