Luogu P2970 [USACO09DEC]自私的放牧
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.
输入输出样例
5
2 4
1 12
4 5
7 10
7 8
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);
}
Luogu P2970 [USACO09DEC]自私的放牧的更多相关文章
- 洛谷 P2970 [USACO09DEC]自私的放牧Selfish Grazing
P2970 [USACO09DEC]自私的放牧Selfish Grazing 题目描述 Each of Farmer John's N (1 <= N <= 50,000) cows li ...
- Luogu P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
- [Luogu P2966][BZOJ 1774][USACO09DEC]牛收费路径Cow Toll Paths
原题全英文的,粘贴个翻译题面,经过一定的修改. 跟所有人一样,农夫约翰以宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道 ...
- 【题解】Luogu P3110 [USACO14DEC]驮运Piggy Back
[题解]Luogu P3110 [USACO14DEC]驮运Piggy Back 题目描述 Bessie and her sister Elsie graze in different fields ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
- Luogu 考前模拟Round. 1
A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...
随机推荐
- IOS 数据存储之 Core Data具体解释
Core Date是ios3.0后引入的数据持久化解决方式,它是是苹果官方推荐使用的.不须要借助第三方框架.Core Date实际上是对SQLite的封装.提供了更高级的持久化方式.在对数据库操作时, ...
- bzoj 1078 [SCOI2008]斜堆 —— 斜堆
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1078 考察斜堆的性质: 一个点如果没有左子树,也一定没有右子树: 看了这篇精美的博客:htt ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- codevs1230元素查找(hash)
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个 ...
- zhw大神线段树姿势
; i<; i++) tree[i][]=tree[i][]=i; ; i>=; i--) tree[i][]=tree[i+i][], tree[i][]=tree[i+i+][]; v ...
- [Apple开发者帐户帮助]六、配置应用服务(6)创建电子钱包标识符和证书
电子钱包提供称为通行证的信息的数字表示- 例如优惠券,演出门票或登机牌 - 允许用户兑换真实世界的产品或服务.您可以通过多种方式使用电子钱包: 选项1:请求,分发和更新通行证 首先注册通行证类型标识符 ...
- HyperLedger Fabric部署与链码解读
1.Fabric简介 Fabric是超级账本中的一个项目,用以推进区块链技术.和其他区块链类似,它也有一个账本,使用智能合约,且是一个参与者可以分别管理自身交易的系统.它是一个联盟链.Fabric与其 ...
- JS/JQuery操作DOM元素笔记
原因 自己目前在搭建一个.NET Core的框架,正在构建权限这块的东西,今天设置权限界面,需要使用JavaScript操作DOM元素,记录一下. 页面大概是酱紫的(我使用的AdminLTE和LayU ...
- 点击文字弹出一个DIV层窗口代码 【或FORM表单 并且获取点击按钮的ID值】
点击不同按钮咨询不同的 专家 <?php for($i=1;$i<5;$i++){ $uid=$i; //用户ID ?> <a class="a_click" ...
- Jenkins-SVN + Maven + Docker
第1步:安装插件 Subversion Plug-inMaven Integration pluginCloudBees Docker Build and Publish pluginDeploy t ...