Description

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

Input

* Line 1: Two integers N and M.

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

Output

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

Sample Input

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.

Sample Output

1
OUTPUT DETAILS: From the last photo, we know that either cow 3 or cow 4 must be spotted.
By choosing either of these, we satisfy the first two photos as well.
——————————————————————————————————
我们用f[i]表示选i这个位置放置特殊点的最优解
那么我们发现每个点可以选择的范围是一个区间
并且容易证明这个区间是随着位置的增加而右移也就是单调递增的
因为你选择了这个点 那么包含这个点的所有区间都不能再加点了
所以r【i】=min(包含i的区间的左端点-1)
因为每个区间都要有点所以l【i】=完整在i左边的区间中左端点的max
这样我们就得到了每个点的转移区间
这样完美符合单调队列的性质 所以就可以写了
#include<cstdio>
#include<cstring>
#include<algorithm>
using std::min;
using std::max;
const int M=,inf=0x3f3f3f3f;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,m,x,y;
int l[M],r[M],f[M];
int q[M],ql=,qr;
int main(){
n=read(); m=read();
for(int i=;i<=n+;i++) r[i]=i-;
for(int i=;i<=m;i++){
x=read(); y=read();
r[y]=min(r[y],x-);
l[y+]=max(l[y+],x);
}
for(int i=n;i;i--) r[i]=min(r[i],r[i+]);
for(int i=;i<=n+;i++) l[i]=max(l[i],l[i-]);
f[qr=]=;
for(int i=;i<=n+;i++){
for(int k=r[i-]+;k<=r[i];k++){
while(ql<=qr&&f[q[qr]]<=f[k]) qr--;
q[++qr]=k;
}
while(ql<=qr&&q[ql]<l[i]) ql++;
if(ql>qr) f[i]=-inf;
else f[i]=f[q[ql]]+;
}
if(f[n+]>=) printf("%d\n",f[n+]-);
else printf("-1\n");
return ;
}

bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp的更多相关文章

  1. BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)

    洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...

  2. bzoj3126[Usaco2013 Open]Photo 单调队列优化dp

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 188[Submit] ...

  3. BZOJ 1499 [NOI2005] 瑰丽华尔兹 | 单调队列优化DP

    BZOJ 1499 瑰丽华尔兹 | 单调队列优化DP 题意 有一块\(n \times m\)的矩形地面,上面有一些障碍(用'#'表示),其余的是空地(用'.'表示).每时每刻,地面都会向某个方向倾斜 ...

  4. bzoj 1499 [NOI2005]瑰丽华尔兹——单调队列优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 简单的单调队列优化dp.(然而当时却WA得不行.今天总算填了坑) 注意滚动数组赋初值应 ...

  5. luogu3084 Photo 单调队列优化DP

    题目大意 农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N.于是约翰拍摄了M(1 <= M <= 100,000 ...

  6. bzoj 3831 Little Bird (单调队列优化dp)

    /*先贴个n*n的*/ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100 ...

  7. ●BZOJ 3126 [Usaco2013 Open]Photo

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...

  8. 单调队列优化DP || [NOI2005]瑰丽华尔兹 || BZOJ 1499 || Luogu P2254

    题外话:题目极好,做题体验极差 题面:[NOI2005]瑰丽华尔兹 题解: F[t][i][j]表示第t时刻钢琴位于(i,j)时的最大路程F[t][i][j]=max(F[t-1][i][j],F[t ...

  9. P4381 [IOI2008]Island(基环树+单调队列优化dp)

    P4381 [IOI2008]Island 题意:求图中所有基环树的直径和 我们对每棵基环树分别计算答案. 首先我们先bfs找环(dfs易爆栈) 蓝后我们处理直径 直径不在环上,就在环上某点的子树上 ...

随机推荐

  1. spring学习(一)——控制反转简单例子

    spring框架是一个开源的轻量级的基于IOC与AOP核心技术的容器框架,主要是解决企业的复杂操作实现. 那IOC与AOP,到底如何解释呢,在看spring视频中,两个专业术语一定必须要懂得. IOC ...

  2. iOS- Apple零配置网络协议Bonjour的使用?

    1.前言 这段时间为了解决公司App的网络离线需求,做了个Apple推出的零配置网络协议Bonjour的Test,主要是为了解决iOS设备的IP获取,之前是可以使用socket的广播来实现,但是使用A ...

  3. Swift 泛型和闭包结合使用

    通常在Swift中定义一个闭包来使用 typealias Closure= (Any?) -> () var tempClosure :Closure? /// 定义一个方法直接调用 func ...

  4. win10 64位系统中安装多个jdk版本的切换问题

    前言: 近期要更换oracle jdk到zulu jdk,因此在本地先安装一版zulu的来进行代码的编译和比较. 注释: 本地电脑之前是oracle jdk 1.8,要更换为zulu jdk 1.8. ...

  5. Python的压缩文件处理 zipfile & tarfile

    本文从以下两个方面, 阐述Python的压缩文件处理方式: 一. zipfile 二. tarfile 一. zipfile 虽然叫zipfile,但是除了zip之外,rar,war,jar这些压缩( ...

  6. c#中语句的先后顺序对结果的影响

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...

  7. Android------BottonTabBar

    前言:一款简单好用封装好的AndroidUI控件,底部导航栏. 1.使用 1.1添加  compile 'com.hjm:BottomTabBar:1.1.1' 1.2  activity_main. ...

  8. 【bzoj3427】Poi2013 Bytecomputer dp

    题目描述 A sequence of N  integers I1,I2…In from the set {-1,0,1} is given. The bytecomputer is a device ...

  9. Kingdom and its Cities - CF613D

    Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in or ...

  10. 【刷题】BZOJ 4698 Sdoi2008 Sandy的卡片

    Description Sandy和Sue的热衷于收集干脆面中的卡片.然而,Sue收集卡片是因为卡片上漂亮的人物形象,而Sandy则是为了积攒卡片兑换超炫的人物模型.每一张卡片都由一些数字进行标记,第 ...