https://www.luogu.org/problemnew/show/P2970

P2970 [USACO09DEC]自私的放牧

题目描述

Each of Farmer John's N (1 <= N <= 50,000) cows likes to graze in a certain part of the pasture, which can be thought of as a large one-dimeensional number line. Cow i's favorite grazing range starts at location S_i and ends at location E_i (1 <= S_i < E_i; S_i < E_i <= 100,000,000).

Most folks know the cows are quite selfish; no cow wants to share any of its grazing area with another. Thus, two cows i and j can only graze at the same time if either S_i >= E_j or E_i <= S_j. FJ would like to know the maximum number of cows that can graze at the same time for a given set of cows and their preferences.

Consider a set of 5 cows with ranges shown below:

  ...                                              ...
... |----|----|----|----|----|----|----|----|----|----|----|----|----
Cow : <===:===> : : :
Cow : <========:==============:==============:=============>:
Cow : : <====> : : :
Cow : : : <========:===> :
Cow : : : <==> : :

These ranges represent (2, 4), (1, 12), (4, 5), (7, 10), and (7, 8), respectively.

For a solution, the first, third, and fourth (or fifth) cows can all graze at the same time. If the second cow grazed, no other cows could graze. Also, the fourth and fifth cows cannot graze together, so it is impossible for four or more cows to graze.

约翰有N(1≤N≤50000)头牛,约翰的草地可以认为是一条直线.每只牛只喜欢在某个特定的范围内吃草.第i头牛喜欢在区间(Si,Ei)吃草,1≤Si<Ei≤1,000,000,00.

奶牛们都很自私,他们不喜欢和其他奶牛共享自己喜欢吃草的领域,因此约翰要保证任意

两头牛都不会共享他们喜欢吃草昀领域.如果奶牛i和奶牛J想要同时吃草,那么要满足:Si>=Ej或者Ei≤Sj.约翰想知道在同一时刻,最多可以有多少头奶牛同时吃草?

输入输出格式

输入格式:

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains the two space-separated integers: S_i and E_i

输出格式:

* Line 1: A single integer representing the maximum number of cows that can graze at once.

输入输出样例

输入样例#1:

5
2 4
1 12
4 5
7 10
7 8
输出样例#1:

3 

不难看出是一道贪心题,应该和线段覆盖差不多。只不过这个求的是最大值。

那么我们首先就应该考虑如何给这些线段排序。

很显然,应该按照右端点排序,右端点小的在前面,因为右端点越小,对之后的线段影响就越小。

下面是一张直观的图。就是按照上面的样例画的。


下面附上代码

#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std; struct haha{
int l, r;
}s[];
int n, now_r, tot; bool cmp(haha a, haha b) {
if(a.r != b.r) return a.r < b.r;
return a.l < b.l;
} int main() {
scanf("%d", &n);
for(int i=; i<=n; i++) {
scanf("%d%d", &s[i].l, &s[i].r);
}
sort(s+, s++n, cmp);
now_r = ;
for(int i=; i<=n; i++) {
if(s[i].l >= now_r) {
now_r = s[i].r;
tot++;
}
}
printf("%d\n", tot);
}
作者:wlz
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

最新文章

  1. 未能加载文件或程序集“System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。
  2. 关于 MAXScript 逐行写入文本
  3. HDU 4679 Terrorist’s destroy
  4. 字符串分割函数 STRTOK &amp; STRTOK_R (转)
  5. UVA 12382 Grid of Lamps 贪心
  6. ubuntu系统分区方案
  7. javascript第二章--变量、作用域和内存问题
  8. Node.js 逐行读取
  9. iOS完整预装字体清单
  10. linux下(fdisk,gdisk,parted)三种分区工具比较
  11. js-图片轮播(极简)
  12. x1c 2017 安装mint18的坑——grub2
  13. python 高阶函数学习, map、reduce
  14. SQL Server孤立账户解决办法
  15. adb push init.rc /
  16. C# 委托和接口
  17. 解决MySQL Slave 触发 oom-killer
  18. Java的容器类
  19. 《Cocos2d-x游戏开发实战精解》学习笔记1--在Cocos2d中显示图像
  20. (转)android从应用到驱动之—camera(1)---程序调用流程

热门文章

  1. chrome浏览器调试线上文件映射本地文件
  2. Java设计模式菜鸟系列(一)策略模式建模与实现
  3. 通达信版F10检索工具下载
  4. 双logo
  5. Android EditText默认不获取焦点
  6. android后台input命令模拟按键【转】
  7. 水晶报表的宽度调整方法(设计器、代码调整、rpt文件属性)
  8. 转 linux/unix学习经典书籍
  9. Sqlserver日期操作
  10. bzoj题目分类