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 . ...
随机推荐
- 自定义View实现图片热区效果
我司主要从事工业物联网领域软件的开发,现有个需求,在外废品处理时需要对产品的不良位置进行标记,点选图片实现图片网格的着色功能. 需求是通过自定义view来实现,实现思路如下: 首先将点击的小方格对象实 ...
- Sequence Models and Long-Short Term Memory Networks
LSTM’s in Pytorch Example: An LSTM for Part-of-Speech Tagging Exercise: Augmenting the LSTM part-of- ...
- RocketMQ配置
安装&配置 1.Clone&Build git clone -b develop https://github.com/apache/incubator-rocketmq.git cd ...
- 自己总结OpenSSL的变化
经过查看openssl源码自带的Makefile,发现: 1) 从0.9.7开始 https://www.openssl.org/source/old/0.9.x/openssl-0.9.7k.tar ...
- Virtualbox使用点滴(共享USB设备,Linux下我的用户没有加到vboxuser中去)
由于网银客户端的问题,只能够在windows环境下支付,所以一直保存着一个激活的virtualbox下的windows,用来完成在线支付. 过去这个激活的windows是安装在ubuntu 10.10 ...
- visual studio添加docker支持简记
很久以前学过一段时间的docker,那时装了电脑卡得受不了,学了一会就卸载了,最近电脑又装上了docker,感觉好像没有以前这么卡了,还是同一台电脑surface pro4, 试了一下visual s ...
- Vbox中Linux虚拟机网络配置(比较实用)
好久没写过东西了,主要大部分都是来自对生活的感悟,很少有实实在在的关于学得有成就感的技术可以“炫耀”,所以也就懒得在这个上面登了. 实验室很早就有位师兄曾在吃饭的路上问过我们这群小弟,你们知道Vbox ...
- python中的内置函数(bytearray)
返回一个新的字节数组.bytearray类是range 0 < = x < 256的一个可变序列.它有大多数可变序列的常用方法,在可变序列类型中描述,以及大多数字节类型的方法,参见字节和B ...
- 【转载】Chrome使用自定义协议打开本地程序并运行IE打开网页
部分内容转载自: http://blog.sina.com.cn/s/blog_e2b8213a0102wqby.html 项目中遇到某需求:chorme要运行IE并打开网页.解决方案之一就是通过自定 ...
- 5分钟学会Java9-Java11的七大新特性
现在Java有多元化的发展趋势,既有JS又有C++还有C#的影子,不学习那是不行滴. 来来来,花5分钟看看Java9-Java11的七大新特性,还有代码样例. Java11 发布了,然而很多公司还在用 ...