Selfish Grazing
程序员文章站
2022-04-02 10:06:32
...
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
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
bool operator<(node m)
{
return y<m.y;
}
}q[50050];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> q[i].x >> q[i].y;
sort(q, q + n);
int flag = q[0].y, ans = 1;
for(int i = 1; i < n; i++)
if(q[i].x >= flag)
flag = q[i].y, ans++;
cout << ans << endl;
return 0;
}
上一篇: 判断两个浮点数是否相等
推荐阅读
-
bzoj 1694 && 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草(DP)
-
[Usaco2005 nov]Grazing on the Run 边跑边吃草 BZOJ1742
-
Selfish Grazing
-
2018.10.22 bzoj1742: Grazing on the Run 边跑边吃草(区间dp)
-
bzoj 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草【区间dp】
-
poj 3042 Grazing on the Run
-
[bzoj1742][Usaco2005 nov][DP]Grazing on the Run 边跑边吃草
-
BZOJ 1742 [Usaco2005 nov]Grazing on the Run 边跑边吃草【区间DP】
-
POJ 3042 Grazing on the Run (三维区间DP)【区间DP模板】
-
UVA10678 The Grazing Cow【椭圆面积】