题目描述

农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N。

于是约翰拍摄了M(1 <= M <= 100,000)张照片,每张照片都覆盖了连续一段奶牛:第i张照片中包含了编号a_i 到 b_i的奶牛。但是这些照片不一定把每一只奶牛都拍了进去。

在拍完照片后,约翰发现了一个有趣的事情:每张照片中都有一只身上带有斑点的奶牛。约翰意识到他的牛群中有一些斑点奶牛,但他从来没有统计过它们的数量。 根据照片,请你帮约翰估算在他的牛群中最多可能有多少只斑点奶牛。如果无解,输出“-1”。

Input

输入输出格式

输入格式:

* Line 1: Two integers N and M.

* Lines 2..M+1: Line i+1 contains a_i and b_i.

输出格式:

* Line 1: The maximum possible number of spotted cows on FJ's farm, or -1 if there is no possible solution.

输入输出样例

输入样例#1: 复制

5 3
1 4
2 5
3 4
输出样例#1: 复制
1
 
 

其他情况均有解


#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long ll;
#define inf 0x3fffffff struct node
{
int l,r;
} a[maxn]; int n,m;
int rp[maxn],lp[maxn];
int q[maxn*],dp[maxn]; int main()
{
// freopen("test.txt","r",stdin);
cin>>n>>m;
for(int i=; i<=n+; i++)
rp[i]=i-;
for(int i=; i<=m; i++)
{
cin>>a[i].l>>a[i].r;
lp[a[i].r+]=max(lp[a[i].r+],a[i].l);
rp[a[i].r]=min(rp[a[i].r],a[i].l-);
}
for(int i=n; i>=; i--)
rp[i]=min(rp[i],rp[i+]);
for(int i=; i<=n+; i++)
lp[i]=max(lp[i],lp[i-]);
int head=;
int tail=;
int j=;
for(int i=; i<=n+; i++)
{
while(j<=rp[i]&&j<=n)
{
if(dp[j]==-)
{
j++;
continue;
}
while(dp[j]>dp[q[tail]]&&tail>=head)
tail--;
q[++tail]=j;
j++;
}
while(q[head]<lp[i]&&head<=tail)
head++;
if(head<=tail)
dp[i]=dp[q[head]]+(i!=n+?:);
else dp[i]=-;
}
if(dp[n+]!=-)
cout<<dp[n+];
else cout<<-; return ;
}

  

P3084 [USACO13OPEN]照片Photo的更多相关文章

  1. 洛谷 P3084 [USACO13OPEN]照片Photo 解题报告

    [USACO13OPEN]照片Photo 题目描述 农夫约翰决定给站在一条线上的\(N(1 \le N \le 200,000)\)头奶牛制作一张全家福照片,\(N\)头奶牛编号\(1\)到\(N\) ...

  2. P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)

    题目链接:传送门 题目: 题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows ( ...

  3. P3084 [USACO13OPEN]照片Photo dp

    题意: 有n个区间,每个区间只能有一个斑点奶牛,问最多有几个斑点奶牛. 思路: 首先要处理出每个点的L[i],R[i]. L[i]表示L[i]-i-1之间一定有一个点.i也是选中的. R[i]表示R[ ...

  4. [USACO13OPEN]照片Photo

    题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N < ...

  5. 洛谷3084 [USACO13OPEN]照片Photo

    原题链接 神仙\(DP\)啊... 题解请移步隔壁大佬的博客\(QAQ\) #include<cstdio> using namespace std; const int N = 2e5 ...

  6. Luugu 3084 [USACO13OPEN]照片Photo

    很神仙的dp...假装自己看懂了,以后回来复习复习... 设$f_{i}$表示从$1$到$i$,且$i$这个点必放的最大数量. 一个区间有两个限制条件:至少放一个,至多放一个. 因为一个区间至多要放一 ...

  7. P3084 [USACO13OPEN]照片(差分约束)

    (已经有了简化版题面) 又秒了一次dp233 本来按照感觉瞎写了一发... 但还是老老实实列式子吧.... 对差分约束有了更深的理解 #include<cstdio> #include&l ...

  8. 洛谷P3084 [USACO13OPEN]照片

    题目 \(DP\) 设状态\(dp[i]\)为\(i\)位置放了斑点牛,前\(i\)个位置能得到的最多的牛. 有方程\(dp[i]=max(dp[j]+1,dp[i])\),而我们并不知道什么\(j\ ...

  9. [USACO13OPEN]照片Photo 题解

    题面 这道题似乎可以用单调队列优化DP做,但这里讲的是一种差分约束的思路; 设s[i]表示1~i中选了多少个: s[b[i]]-s[a[i]-1]<=1; s[b[i]]-s[a[i]-1]&g ...

随机推荐

  1. js-ES6学习笔记-async函数

    1.async 函数是 Generator 函数的语法糖.前文有一个 Generator 函数,依次读取两个文件. var fs = require('fs'); var readFile = fun ...

  2. 【代码笔记】iOS-UIAlertView3秒后消失

    一,效果图. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the ...

  3. Oracle EBS INV 查询物料无值 ECO

    查找物料的时候报错 没有输入值 解决方法: 针对FORM做trace 多查看几个生成的trace 搜索 MTL_SYSTEM_ITEMS_b 的信息 查看到最后面的语句(一般可直接查看) 看SQL 哪 ...

  4. SQL Server 子查询错误:No column name was specified for column 2 of 'a' error (转载)

    问: I have a MySQL query and I ran it working fine but same query showing error in SQL Server. SQL Se ...

  5. myeclipse配置jboss

    一 相关软件准备 jdk1.6     Myeclipse    jboss 6.1 下载地址:http://www.jboss.org/jbossas/downloads/二 安装 下载完jboss ...

  6. systemd 之 journalctl

    Systemd 日志系统 一.前言 昨天写了一篇文章,内容为:Systemd 常规操作与彩蛋,参考了 ArchLinux 官方文档并结合培训中的思路进行了部分修改补充.如果你懂得了基础的管理,那必然还 ...

  7. Operating System Error Codes

    How To Fix Windows Errors Click here follow the steps to fix Windows and related errors. Instruction ...

  8. VS2015 调试 条件和操作设置

    vs 开发过程中,经常会遇到这样的场景: 例如有几千条数据,调试代码的时候需要在循环中走到name="A"的这条数据, 并且单步调试这条数据走完整个逻辑过程.这个时候调试的条件设置 ...

  9. Georgia Tech Online Master of Science in Computer Science 项目经验分享

    Georgia Tech Online Master of Science in Computer Science 项目经验分享 Posted on 2014/04/22 项目关键词:工科名校,计算机 ...

  10. Software Development Engineer, RDS Database Engines, Seattle

    DESCRIPTION About UsAmazon Aurora is an exciting new area of innovation for AWS, and the PostgreSQL- ...