洛谷 P2970 [USACO09DEC]自私的放牧Selfish Grazing
题目描述
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:
... 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
... |----|----|----|----|----|----|----|----|----|----|----|----|----
Cow 1: <===:===> : : :
Cow 2: <========:==============:==============:=============>:
Cow 3: : <====> : : :
Cow 4: : : <========:===> :
Cow 5: : : <==> : :
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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,v,ans;
struct nond{
int a,b;
}f[];
bool cmp(nond x,nond y){
return x.b<y.b;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&f[i].a,&f[i].b);
sort(f,f+n,cmp);
for(int i=;i<=n;i++)
if(i==||f[i].a>=f[v].b){ ans++;v=i; }
printf("%d\n",ans);
}
洛谷 P2970 [USACO09DEC]自私的放牧Selfish Grazing的更多相关文章
- Luogu P2970 [USACO09DEC]自私的放牧
https://www.luogu.org/problemnew/show/P2970 P2970 [USACO09DEC]自私的放牧 题目描述 Each of Farmer John's N (1 ...
- 洛谷 P2969 [USACO09DEC]音符Music Notes
P2969 [USACO09DEC]音符Music Notes 题目描述 FJ is going to teach his cows how to play a song. The song cons ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
- BZOJ3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 47 Solve ...
- 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 71 Solve ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
随机推荐
- Jquery学习总结(3)——Jquery获取当前城市的天气信
Jquery代码: function findWeather() { var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup ...
- sql查询语句中on和where的区别
sql中的连接查询分为3种, cross join,inner join,和outer join , 在 cross join和inner join中,筛选条件放在on后面还是where后面是没区别 ...
- ubuntu设置PATH
试了好多遍,多无效.. 最后在/etc/enviroment下设置才有效. 不过让有一些未解问题 我使用sudo su 进入到root用户权限,设置完成的. 重新使用sudo -s进入root用户权限 ...
- POJ——T1679 The Unique MST
http://poj.org/problem?id=1679 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30120 ...
- [AngularJS]Chapter 8 秘籍诀窍
<!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> <met ...
- HDU 4321 Contest 3
题意:给定a和b,n,让你求b+a, b+2*a, .......b+n*a里面有多少1. 当统计第K位的时候 可以注意到 第 B+T*A 和 B+(T+2^(K+1))*A 位是相同的 那么 第K位 ...
- HTML5简单进度环插件
前几天做了一个进度条的插件.今天我用HTML5的arc做一个简单的进度环的插件. 代码演示 事实上非常easy的.相同,我们先用一个实例: 配置js代码 var setting = { id: &qu ...
- setsockopt 设置socket
1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:BOOL bReuseaddr=TRUE;setsockopt(s,SOL_SOCKET ,SO ...
- httpClient模拟登陆校内某系统
package com.huowolf; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpE ...
- jquery简介 each遍历 prop attr
一.JQ简介 jQuery是一个快速.简洁的JavaScript框架,它封装了JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作.事件处理.动画设计和 ...