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<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<queue> using namespace std; int n,t,ans=1; struct node{ int x,y; }a[50005]; int read(){ int a=0,b=1; char ch=getchar(); while((ch<48||ch>57)&&ch!='-'){ ch=getchar(); } if(ch=='-'){ b=-1; ch=getchar(); } while(ch<48||ch>57){ ch=getchar(); } while(ch>47&&ch<58){ a=a*10+ch-48; ch=getchar(); } return a*b; } bool cmp(node x,node y){ return x.y<y.y; } int main(){ n=read(); for(int i=1;i<=n;i++){ a[i].x=read(),a[i].y=read(); } sort(a+1,a+n+1,cmp); t=a[1].y; for(int i=2;i<=n;i++){ if(a[i].x>=t){ t=a[i].y; ans++; } } printf("%d",ans); return 0; }
原文地址:https://www.cnblogs.com/xiongchongwen/p/11567003.html
- Kotlin语法基础之运算符
- Kotlin语法基础之控制流
- Kotlin 包和 import 语句使用
- 使用多个Target去管理项目版本环境
- Facebook iOS UI 工具ComponentKit简介
- 基于OpenCV的跳一跳外挂实现原理
- React Native 0.50版本新功能简介
- Android滤镜效果实现及原理分析
- Spring Boot入门及整合mybatis
- Swift开发React Native组件
- 深入理解Android插件化技术
- Kotlin DSL详解
- 大规模异常滥用检测:基于局部敏感哈希算法——来自Uber Engineering的实践
- 事务与一致性:刚性or柔性?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 正确使用 wait/notify/notify方法以及源码解析
- 秒杀商品超卖事故:Redis分布式锁请慎用!
- 3D开发是一个生态,ThingJS支持js,css,json,html外部资源引用
- 手把手教你实现xxl-job分布式任务调度平台搭建
- 在 vscode 中 debugger 调试
- MongoDB 的安装
- 一次opencanary自定义实践
- 从0到1编写一个Xposed Module :Anti Screenshot
- Linux pwn入门学习到放弃
- 一款针对网站的网络侦察和信息收集工具
- Numpy核心语法和代码整理汇总!
- 发现内网存活主机的各种姿势
- SharedPreferences VS MMKV
- RabbitMQ 最常用的 3 大模式!
- 啥?HashMap 1.8 还有死循环?你逗我呢!