Selfish Grazing(贪心 区间覆盖)

Selfish Grazing

https://ac.nowcoder.com/acm/problem/24867

题目描述

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 Si and ends at location Ei (1 <= Si < Ei; Si < Ei <= 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 Si >= Ej or Ei <= Sj. 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.

输入描述:

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains the two space-separated integers: Si and Ei

输出描述:

* 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 

输出

3

题意

给定若干个区间,找出最多有几个互相不重叠的区间

思路

对区间右端点进行从小到大排序,可以这样理解,结束得越早,我更有机会开始下一个新区间

代码

//Selfish Grazing(贪心 区间覆盖)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 50010;

struct S
{
	int l;
	int r;
};

int n;
S s[N];

bool cmp(S a , S b)
{
	return a.r < b.r;
}

int main()
{
 	scanf("%d" , &n);
 	for(int i = 0 ; i < n ; i++)
 		scanf("%d %d" , &s[i].l , &s[i].r);
 	
 	sort(s , s + n , cmp);
 	
 	int cnt = 1 , now = s[0].r;
 	for(int i = 1 ; i < n ; i++)
 	{
		if(s[i].l >= now)
		{
			cnt++;
			now = s[i].r;	
		} 
	}
	
	printf("%d\n" , cnt);
	return 0;
}

入门课第一节习题题解

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
13730次浏览 132人参与
# AI面会问哪些问题? #
822次浏览 20人参与
# 巨人网络春招 #
11461次浏览 224人参与
# 你的实习产出是真实的还是包装的? #
2431次浏览 47人参与
# AI时代,哪个岗位还有“活路” #
2509次浏览 49人参与
# 长得好看会提高面试通过率吗? #
2446次浏览 39人参与
# MiniMax求职进展汇总 #
24619次浏览 313人参与
# 你做过最难的笔试是哪家公司 #
1029次浏览 18人参与
# HR最不可信的一句话是__ #
921次浏览 31人参与
# 沪漂/北漂你觉得哪个更苦? #
935次浏览 29人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7911次浏览 43人参与
# XX请雇我工作 #
51130次浏览 171人参与
# 简历中的项目经历要怎么写? #
310766次浏览 4252人参与
# 简历第一个项目做什么 #
31981次浏览 354人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152732次浏览 888人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187495次浏览 1123人参与
# AI时代,哪些岗位最容易被淘汰 #
64398次浏览 857人参与
# 如果重来一次你还会读研吗 #
229942次浏览 2011人参与
# 正在春招的你,也参与了去年秋招吗? #
364041次浏览 2640人参与
# 腾讯音乐求职进展汇总 #
160797次浏览 1114人参与
# 你怎么看待AI面试 #
180538次浏览 1287人参与
# 投格力的你,拿到offer了吗? #
178053次浏览 889人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务