3126: [Usaco2013 Open]Photo

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 374  Solved: 188
[Submit][Status][Discuss]

Description

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个区间,每个区间里有且仅有一个点,问能有多少个点

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.

HINT

 

Source

Gold

定义f[i]前i个点 满足条件 最多能选多少个
f[i]=f[j]+1 j∈[l,r]
需要判断的是j的区间,即i从哪里转移
1.每个区间有一个点 可以确定l是不包含i点且在完全在i点左边的区间的最靠右的左端点
2.每个区间仅有一个点 可以确定r是包含i点的区间最靠左的左端点-1
可以单调队列维护 也可以数据结构维护

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 200050
using namespace std;
int n,m,f[N],q[N],l[N],r[N];
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n+;i++)r[i]=i-;
for(int i=;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
r[b]=min(r[b],a-);
l[b+]=max(l[b+],a);
}
for(int i=;i<=n+;i++)l[i]=max(l[i],l[i-]);
for(int i=n;i>=;i--)r[i]=min(r[i],r[i+]);
int h=,t=,j=;
for(int i=;i<=n+;i++){
while(j<=r[i]&&j<=n){
if(f[j]==-){j++;continue;}
while(h<=t&&f[j]>=f[q[t]])t--;
q[++t]=j++;
}
while(h<=t&&q[h]<l[i])h++;
if(h<=t)f[i]=f[q[h]]+;
else f[i]=-;
}
printf("%d\n",f[n+]==-?-:f[n+]-);
return ;
}

bzoj3126[Usaco2013 Open]Photo 单调队列优化dp的更多相关文章

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

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

  2. bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp

    Description 给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点 Input * Line 1: Two integers N and M. * Lines 2..M+ ...

  3. luogu3084 Photo 单调队列优化DP

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

  4. 动态规划专题(四)——单调队列优化DP

    前言 单调队列优化\(DP\)应该还算是比较简单容易理解的吧,像它的升级版斜率优化\(DP\)就显得复杂了许多. 基本式子 单调队列优化\(DP\)的一般式子其实也非常简单: \[f_i=max_{j ...

  5. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  6. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  7. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

  8. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  9. BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP

    BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...

随机推荐

  1. 【Java每日一题】20161220

    package Dec2016; public class Ques1220 { public static void main(String[] args) { Integer num1 = new ...

  2. JSP三大指令、七大动作、九大对象

    <%---------------------- JSP三大指令 -----------------------------%><%-- 1. page: language impo ...

  3. mybatis组合模糊+分页查询

    //组合模糊查询就是这么简单 <select id="findAllJiemu" parameterType="java.util.Map" result ...

  4. oracle结构与安全

    从宏观上来看:oracle数据库服务器主要有数据库和实例组成. 在orale数据库服务器中,首先会产生一个实例,通过实例访问一个数据库. 一个实例对应着一个数据库. oracle 数据库在逻辑上是按层 ...

  5. LeetCode House Robber

    原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...

  6. 读&lt;你必须知道的.NET&gt;IL指令笔记

    IL指令笔记: 1.newObj和initObj MSDN解释:newObj用于分配和初始化对象,而initObj用户初始化值类型 newObj解释: (1):从托管堆分配指定类型所需要的全部内存空间 ...

  7. 访问远程mysql数据库

    使用mysql命令窗口模式/工具,比如需要给'10.2.9.239' 的用户分配mantis123,mantis123访问,则使用如下格式: GRANT ALL PRIVILEGES ON *.* T ...

  8. 外设:K9F2G08 nandflash 底层读写、控制驱动程序,可随机读写

    /****************************************************************************** Copyright (C), 2001- ...

  9. LintCode 521.去除重复元素

    LintCode 521.去除重复元素 描述 给一个整数数组,去除重复的元素. 你应该做这些事 1.在原数组上操作 2.将去除重复之后的元素放在数组的开头 3.返回去除重复元素之后的元素个数 挑战 1 ...

  10. ubuntu-16.04更好软件源

    author: headsen chen date:2019-03-06  14:01:07 1,修改软件源文件成如下的清华大学的源(亲测可用) root@ubuntu:/var/lib/apt/li ...