bzoj3126[Usaco2013 Open]Photo 单调队列优化dp
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
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
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
定义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的更多相关文章
- BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)
洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...
- bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp
Description 给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点 Input * Line 1: Two integers N and M. * Lines 2..M+ ...
- luogu3084 Photo 单调队列优化DP
题目大意 农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N.于是约翰拍摄了M(1 <= M <= 100,000 ...
- 动态规划专题(四)——单调队列优化DP
前言 单调队列优化\(DP\)应该还算是比较简单容易理解的吧,像它的升级版斜率优化\(DP\)就显得复杂了许多. 基本式子 单调队列优化\(DP\)的一般式子其实也非常简单: \[f_i=max_{j ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- 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 ...
- hdu3401:单调队列优化dp
第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP
BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...
随机推荐
- 洛谷P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...
- AWK读书笔记
1.awk 'parttern {action}' filename 从文件中逐行读取并匹配parttern,若匹配成功执行action否则读取下一行. parttern和action都可选,若省略p ...
- ThinkPad安装deepin操作系统报错解决方法
目前deepin操作系统,软件也比较多,所以想在自己的thinkpad t430笔记本上安装.但是安装时报错,具体错误忘了看了.反复试了好几次都不行,最后在网上查了,讲bios设置调整之后可以正常安装 ...
- LeetCode & Q283-Move Zeroes-Easy
Array Two Pointers Description: Given an array nums, write a function to move all 0's to the end of ...
- Ansible自动化运维工具-上
[Ansible特点] 1)Ansible与saltstack均是基于Python语言开发的 2)安装使用简单,基于不同插件和模块实现各种软件,平台,版本的管理以及支持虚拟容器多层级的部署 3)不需要 ...
- SpringCloud的应用发布(一)SpringCloud的样例工程
前言 这个综合例子创建了 6个微服务应用 一个服务注册中心 SvcReg(EurekaServer),生产中要考虑高可用 一个配置中心 CfgMgr + git目录存储配置(ConfigServer, ...
- hadoop2.6.0实践:引入开发依赖的jar包
hadoop-2.5.0\share\hadoop\common 所有jar,hadoop-2.5.0\share\hadoop\common\lib 所有jar,hadoop-2.5.0\sha ...
- linux下的Shell编程(5)循环
Shell Script中的循环有下面几种格式: while [ cond1 ] && { || } [ cond2 ] -; do - done for var in -; do - ...
- Spring Security入门(3-3)Spring Security 手工配置并注入 authenticationProvider 和 异常信息传递
特别注意的是 这样就能保证抛出UsernameNotFoundException时,前台显示出错信息: 另外,ps:
- Python之函数基础
1.函数的定义与调用 函数从大方针上考虑总共分为两种:一种是内置函数,另一种是自定义函数.今天主要讲的是自定义函数. s = '金老板小护士' #len(s) def my_len(): #自定义函数 ...