跳到正文

个人记录

[USACO09DEC] Selfish Grazing S

w20130311约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

思路:一道关于区间的简单贪心,但是有些迷惑性。很多人一看到题目就把数据按左端点排序,但是应当按右边端点,因为要求最多牛吃草时刻,所以看这头牛的结束时间,每次用开始时间比较,并更新计算器。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=50005;
int n,ans=1,now;

struct node {
	int x,y;
}a[N];

bool cmp (node x,node y) {
    return x.y<y.y;
}
int main () {
	cin>>n;
	for (int i=1;i<=n;i++)
		cin>>a[i].x>>a[i].y;
	sort (a+1,a+1+n,cmp);
	now=a[1].y;
	for (int i=2;i<=n;i++) {
		if (a[i].x>=now) {
			ans++;
			now=a[i].y;
		}
	}
	cout<<ans;
	return 0;
}

原文