Bzoj 3126[Usaco2013 Open]Photo 题解
3126: [Usaco2013 Open]Photo
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 335 Solved: 169
[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
#pragma GCC optimze("O3")
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#define N 200005
using namespace std;
int n,m,r[N],l[N],q[N*],en,hea=,f[N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n+;i++)
r[i]=i-;
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
l[y+]=max(l[y+],x);
r[y]=min(r[y],x-);
}
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+]);
en++;
q[en]=;
for(int i=;i<=n+;i++)
{
for(int j=r[i-]+;j<=r[i];j++)
{
while(hea<=en&&f[q[en]]<f[j])en--;
en++;
q[en]=j;
}
while(hea<=en&&q[hea]<l[i])hea++;
if(hea>en)
f[i]=-0x7ffffff;
else
f[i]=f[q[hea]]+;
}
if(f[n+]>=)printf("%d\n",f[n+]-);
else
printf("-1\n");
return ;
}
Bzoj 3126[Usaco2013 Open]Photo 题解的更多相关文章
- 数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo
3126: [Usaco2013 Open]Photo Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 222 Solved: 116 Descrip ...
- ●BZOJ 3126 [Usaco2013 Open]Photo
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...
- bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp
Description 给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点 Input * Line 1: Two integers N and M. * Lines 2..M+ ...
- BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)
洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...
- bzoj3126[Usaco2013 Open]Photo 单调队列优化dp
3126: [Usaco2013 Open]Photo Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 374 Solved: 188[Submit] ...
- [bzoj 3048] [Usaco2013 Jan]Cow Lineup
[bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...
- BZOJ 4033: [HAOI2015]树上染色题解
BZOJ 4033: [HAOI2015]树上染色题解(树形dp) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327400 原题地址: BZOJ 403 ...
- [BZOJ 3126] Photo
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3126 [算法] 差分约束系统 注意SPFA判负环的条件应为 : 若所有点入队次数之和 ...
- bzoj 4094: [Usaco2013 Dec]Optimal Milking
4094: [Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 . ...
随机推荐
- git服务器创建,冲突解决,远程仓库获取指定文件
1.git服务器创建 在公司多人协作开发的情况下,不能简单地使用github,因为github是互联网公开的,这种情况公司的代码的保密性就会丧失了.这种情况下,需要创建git服务器. 登录服务器,使用 ...
- RxJava入门优秀博客推荐
RxJava用了快半年了,现在越来越离不开这个库,从使用到逐渐接触它的背后实现,突然想写点什么关于RxJava的内容.在酝酿如何组织内容的时候,就去看看自己关于RxJava的收藏,发现满满的干货! 1 ...
- ORA-13541: system moving window baseline size (691200) greater than retention (432000)
修改awr生成报告时间间隔和保存时间时报错,由默认的每小时生成,保存8天修改为每半个小时生成一次,保存5天: SQL, retention); , retention); END; * ERROR a ...
- stdlib.h,string.h,wchar.h的函数列表(cplusplus.com就有,很清楚)goodx
Multibyte characters mblen Get length of multibyte character (function ) mbtowc Convert multibyte se ...
- C#基础原理拾遗——引用类型的值传递和引用传递
以前写博客不深动,只搭个架子,像做笔记,没有自己的思考,也没什么人来看.这个毛病得改,就从这一篇开始- 最近准备面试,深感基础之重要,奈何我不是计算机科班出身,基础方面有些捉襟见肘.短期怎么补?做面实 ...
- Redis EXISTS命令耗时过长case排查
一.背景 redis慢日志分析平台上线后,随便看了一下,发现onestore使用的缓存集群,存在大量的EXISTS命令慢查询的情况: 平均每个EXISTS命令需要13ms,最大耗时近20ms.这个结果 ...
- git上如何处理无法clone和merge
对于一些需要FQ才能克隆下来的项目,我们需要使用代理 进入terminal: 设置代理: git config --global http.proxy http://127.0.0.1:1087 gi ...
- 【Flink】Flink 底层RPC框架分析
1. 前言 对于Flink中各个组件(JobMaster.TaskManager.Dispatcher等),其底层RPC框架基于Akka实现,本文着重分析Flink中的Rpc框架实现机制及梳理其通信流 ...
- 移动IM开发指南1:如何进行技术选型
<移动IM开发指南>系列文章将会介绍一个IM APP的方方面面,包括技术选型.登陆优化等.此外,本文作者会结合他在网易云信多年iOS IM SDK开发的经验,深度分析实际开发中的各种常见问 ...
- spring cloud 系列第7篇 —— sleuth+zipkin 服务链路追踪 (F版本)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.简介 在微服务架构中,几乎每一个前端的请求都会经过多个服务单元协调来提 ...