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 . ...
随机推荐
- 在云中生成和模拟 iOS
原文:在云中生成和模拟 iOS 1.原文地址 https://msdn.microsoft.com/zh-cn/library/vs/alm/dn858446.aspx
- SQL 修改主键约束
原文:SQL 修改主键约束 今天在学习数据库的时候遇到一个关于如何修改主键约束的问题,抄录下来以供备用. --修改主键约束用SQL --获取主键约束名字 declare @csname varchar ...
- UWP开发学习笔记1
导航到页面: this.Frame.Navigate(typeof(SecondPage)); 导航进入当前页面时会调用OnNavigatedTo方法:导航从当前页面离开时会调用OnNavigatin ...
- C#管理服务停止启动
由于机器性能问题,把许多服务关闭了,需要用的时候再开启,这样每次都打开服务管理或cmd命令比较麻烦.就自己写了工具显示在桌面上; 声明:ServiceController myController = ...
- Leaflet(Esri)初识
加载本地地图 <html> <head> <metacharset=utf-8/> <title>IdentifyingFeatures</tit ...
- Android实现简单音乐播放器(startService和bindService后台运行程序)
Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能有 ...
- 利用docker在window7下安装TensorFlow
安装过程下碰了不少坑,记录一下安装过程,方便以后有需要时复用. 1.安装docker 下载最新版本的docker并且默认安装即可,安装后打开Docker Quickstart Terminal,初次进 ...
- Office Add-in Model 为 Outlook Mail Add-in 提供的 JavaScript API 介绍
本文所讨论的 Mailbox API是指在 Mail Add-in 中可调用的 JavaScript API.开发者可以利用这些API 实现 Add-in 和 Outlook 的交互(数据读取与写入) ...
- 深入解析Windows窗口创建和消息分发(三个核心问题:怎么将不同的窗口过程勾到一起,将不同的hwnd消息分发给对应的CWnd类去处理,CWnd如何简单有效的去处理消息,由浅入深,非常清楚) good
笔记:争取不用看下面的内容,只看自己的笔记,就能记住这个流程,就算明白了: _tWinMain-->AfxWinMain,它调用四个函数: -->AfxWinInit用于做一些框架的初始化 ...
- javaweb各种框架组合案例(二):maven+spring+springMVC+mybatis
1.mybatis是比较新的半自动orm框架,效率也比较高,优点是sql语句的定制,管理与维护,包括优化,缺点是对开发人员的sql功底要求较高,如果比较复杂的查询,表与表之间的关系映射到对象与对象之间 ...