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

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
 
  不知道对于这道题有没有人和我一样,想到了 [Apio2012]Guard 这道题,题目设置还是挺类似的,然而正解之间貌似毫不相关。
  这道题更多的是偏向于动归题,如果知道是动归的话那么转移方程一般也就写的出来了:
    f[i]=MAX(f[j])+1
  看着挺简单的,但是j的取值范围的确定是这道题的关键,为此我们可以在纸上自己模拟一些情况,然后我们可以注意到,能够更新这个点且合法的点一定在离当前点最近的完整区间的左端点及其之后,且一定在离当前点最近的右端点之前。而我们又发现这个区间对于每个点都是单调的,所以我们可以打出来单调队列来进行优化。因此这道题就差不多搞完了。
  最后一点,不合法的情况的判断。如果说当前点的可选取的左边界大于右边界,那么,这个点我们是不能去放置的,我们应把它赋为极小值,注意,此时只是在这个点放置不合法,不代表整个题都不合法。而如果出现了对于整个题目设置都不合法的情况他后面的区间都会受他影响而变为负值。这一点语言不好表述,请读者自行手推。
 #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 题解的更多相关文章

  1. 数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 222  Solved: 116 Descrip ...

  2. ●BZOJ 3126 [Usaco2013 Open]Photo

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...

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

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

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

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

  5. bzoj3126[Usaco2013 Open]Photo 单调队列优化dp

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 188[Submit] ...

  6. [bzoj 3048] [Usaco2013 Jan]Cow Lineup

    [bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...

  7. BZOJ 4033: [HAOI2015]树上染色题解

    BZOJ 4033: [HAOI2015]树上染色题解(树形dp) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327400 原题地址: BZOJ 403 ...

  8. [BZOJ 3126] Photo

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3126 [算法] 差分约束系统 注意SPFA判负环的条件应为 : 若所有点入队次数之和 ...

  9. bzoj 4094: [Usaco2013 Dec]Optimal Milking

    4094: [Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 . ...

随机推荐

  1. MFC应用程序配置不正确解决方案(manifest对依赖的强文件名,WinSxs是windows XP以上版本提供的非托管并行缓存)

    [现象] 对这个问题的研究是起源于这么一个现象:当你用VC++2005(或者其它.NET)写程序后,在自己的计算机上能毫无问题地运行,但是当把此exe文件拷贝到别人电脑上时,便不能运行了,大致的错误提 ...

  2. 浅谈Android高通(Qualcomm)和联发科(MTK)平台

    一款CPU好不好是要从多个方面考虑的,并不是说简简单单看一个主频.几个核心数就完了,更重要的是它的综合实力到底有多强,这里面当然也会牵扯到价格问题,性能相似当然是便宜的获胜,这是毋庸置疑的. 事实上, ...

  3. 深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数

    原文:深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数 概述 COLUMNS_UPDATED函数能够出现在INSERT或UPDATE触发器中AS关键字后的任何位置,用来 ...

  4. 【转】编程之道 之 Rob Pike

    1.你无法断定程序会在什么地方耗费运行时间.瓶颈经常出现在想不到的地方,所以别急于胡乱找个地方改代码,除非你已经证实那儿就是瓶颈所在. 2.估量.在你没对代码进行估量,特别是没找到最耗时的那部分之前, ...

  5. SqlServer判断数据库、表、字段、存储过程、函数是否存在

    原文:SqlServer判断数据库.表.字段.存储过程.函数是否存在 判断数据库是否存在 if exists (select * from sys.databases where name = '数据 ...

  6. Android零基础入门第87节:Fragment添加、删除、替换

    前面一起学习了Fragment的创建和加载,以及其生命周期方法,那么接下来进一步来学习Fragment的具体使用,本期先来学习Fragment添加.删除.替换. 一.概述 在前面的学习中,特别是动态加 ...

  7. 解决com.android.dex.DexException: Multiple dex files define Lcom/google/gson/JsonSerializer;

    我在开发Windows Azure的Mobile Service(隔天补上创建过程)的安卓客户端时,报出了com.android.dex.DexException: Multiple dex file ...

  8. Qt技术优势

    1. Qt这个C++的图形库由Trolltech在1994年左右开发.它可以运行在Windows,Mac OS X, Unix,还有像Sharp Zaurus这类嵌入式系统中.Qt是完全面向对象的. ...

  9. mysql自动安装教程说明

    这里只说明了思路和方法 我们在安装程序里面可能需要安装的时候将mysql一起安装,那么我们就按照下面的顺序思路来. 首先我们安装的电脑上可能已经安装了mysql,所以我们的mysql服务就起一个名字, ...

  10. Dependency Injection 筆記 (1)

    <.NET 依賴注入>連載 (1) 本文从一个基本的问题开始,点出软件需求变动的常态,以说明为什么我们需要学习「依赖注入」(dependency injection:简称 DI)来改善设计 ...