题目描述

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(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

说明

There are 5 cows and 3 photos. The first photo contains cows 1 through 4, etc.

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.

题解

  • 真的是卡spfa卡的不亦乐乎!
  • 题目大意:给定n个区间,每个区间中只有一头斑点牛,求最多可能有多少头斑点牛
  • 每个区间最少只有一头斑点牛,也最多只有一头斑点牛
  • 那么一只奶牛要不就是斑点牛,要不就是正常牛,那么就是0<=sum[i]-sum[i-1]<=1
  • 转移一下sum[R]-sum[L-1]<=1,sum[L-1]-sum[R]<=-1
  • 那么就可以差分约束,把i和i-1之间连一条0边,i-1和i之间连一条1边,a-1和b之间连一条1边,b和a-1之间连一条-1边
  • 如何跑一个优秀的spfa才是重点,可以用双向队列优化spfa

代码

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4 #define N 200010
 5 #define inf 0x7fffffff
 6 using namespace std;
 7 struct edge {int to,from,v;}e[N*4];
 8 int n,m,vis[N*2],dis[N*2],head[N*2],cnt=1,tot;
 9 void insert(int x,int y,int v) { e[++cnt].to=y,e[cnt].from=head[x],e[cnt].v=v,head[x]=cnt; }
10 int spfa()
11 {
12     deque<int> Q;
13     for (int i=1;i<=n;i++) dis[i]=inf;
14     vis[0]=1,Q.push_back(0);
15     while (Q.size())
16     {
17         int u=Q.front(); Q.pop_front(),vis[u]=0;
18         for (int i=head[u];i;i=e[i].from)
19             if (dis[e[i].to]>dis[u]+e[i].v)
20             {
21                 dis[e[i].to]=dis[u]+e[i].v;
22                 if (!vis[e[i].to])
23                 {
24                     if (++tot>1926817) return -1;
25                     vis[e[i].to]=1;
26                     if (Q.size()&&dis[e[i].to]>dis[Q.front()]) Q.push_back(e[i].to); else Q.push_front(e[i].to);
27                 }
28             }
29     }
30     return dis[n];
31 }
32 int main()
33 {
34     scanf("%d%d",&n,&m);
35     for (int i=1,l,r;i<=m;i++) scanf("%d%d",&l,&r),insert(l-1,r,1),insert(r,l-1,-1);
36     for (int i=1;i<=n;i++) insert(i-1,i,1),insert(i,i-1,0);
37     printf("%d\n",spfa());
38 }

转载于:https://www.cnblogs.com/Comfortable/p/10301176.html

[spfa][差分约束] 洛谷 P3084 照片Photo相关推荐

  1. 【题解】洛谷P3084 照片(差分约束)

    https://www.luogu.org/blog/user9643/solution-p3084 注意用优先队列..还有判断负环

  2. 洛谷 P3084 [USACO13OPEN]照片Photo

    https://daniu.luogu.org/problem/show?pid=3084 单调性dp 我靠你看到这个题目,怎么看都和查分约束有点关系对不对: 估计是我差分约束学的不好,也找不到为什么 ...

  3. Schedule Problem spfa 差分约束

    题意:有n个任务,给出完成n个任务所需时间,以及一些任务安排.任务安排有四种: FAS a b:任务a需在任务b开始后完成. FAF a b:任务a需在任务b完成后完成. SAF a b:任务a需在任 ...

  4. (差分)洛谷P4231 三步必杀

    洛谷P4231 三步必杀 三步必杀 题目背景 (三)旧都 离开狭窄的洞穴,眼前豁然开朗. 天空飘着不寻常的雪花. 一反之前的幽闭,现在面对的,是繁华的街市,可以听见酒碗碰撞的声音. 这是由被人们厌恶的 ...

  5. SPFA差分约束(bzoj 2330: [SCOI2011]糖果)

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 6355  Solved: 2096 [Submit][Stat ...

  6. #0/1分数规划,SPFA,负环#洛谷 1768 天路

    题目链接 分析 可以发现,这是一道0/1分数规划的题目,需要二分答案,然后判定是否存在负环,所以说bfs貌似不高效,那就用dfs吧,若二分答案为0,即不合法 代码 #include <cstdi ...

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

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

  8. P3084 [USACO13OPEN]照片Photo 差分约束

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

  9. 洛谷P7515:矩阵游戏(差分约束)

    解析 如果没有元素均要在 [0,1e6][0,1e6][0,1e6] 的条件,可以很容易的构造出一个合法解. 那么我们就要通过调整得到的解,使所有数都在合法范围内. 注意到,每次给某一行/列依次+1, ...

最新文章

  1. 【转载】mysql慢查询
  2. java调用天气预报api_java调用中国天气网api获得天气预报信息
  3. SpringCloud 介绍
  4. mysql新建库和用户linux_Linux环境 Mysql新建用户和数据库并授权
  5. 《SpringBoot揭秘:快速构建微服务体系》—第3章3.5节本章小结
  6. 技术人生:希望有生之年开发一个“自己的解释语言”
  7. VBS教程-wscript对象
  8. python爬虫破解大众点评的字体加密
  9. 医院门诊管理系统——计件收费
  10. Maven解决版本冲突的问题
  11. 计算机网络教学改革方案,三教改革下的计算机网络专业教学改革
  12. python里sample_python中的sample什么意思
  13. zjb_integrated 的BLOG(学习DaVinci的好文章)
  14. 今日恐慌与贪婪指数为25 等级转为极度恐慌
  15. Excel应用{数据加工与公式函数}
  16. 今天,我宣布了新 Logo
  17. oracle雾化试图_ORACLE物化视图具体解释
  18. 计算机类SCI与EI收录的外文期刊
  19. Socket基本操作的C++封装--以及Socket通信实践
  20. 重心在哪里(简单数学题)

热门文章

  1. deebot扫地机器人怎么清洁_智能清洁小助手开始工作 360扫地机器人S7评测
  2. unity 继承会调用start吗_【浅入浅出】Unity 雾效
  3. python文件式_python中文件操作的六种模式及对文件某一行进行修改的方法
  4. python制作中秋贺卡_中秋节到了,送你一个Python做的Crossin牌“月饼”
  5. java深度克隆_Java深入学习26:Java深度克隆
  6. ttf能改成gfont吗_如何编辑ttf字体文件
  7. c语言块级作用域_块级作用域问题ES5 ES6
  8. kl散度定义_Kullback-Leibler(KL)散度介绍
  9. 观察者设计模式 php,PHP设计模式 - 观察者模式
  10. mysql下载备份数据库命令行,如何从MariaDB数据库备份和还原命令行