USACO09DEC Selfish Grazing

题目链接

题解

贪心入门题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef pair<int,int> P;
P p[50005];
int main(){
int n,i,j,u,v,ans=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&u,&v),
p[i].first=v,p[i].second=u;
sort(p,p+n);
ans++,u=p[0].second,v=p[0].first;
for(i=1;i<n;){
while(i<n&&p[i].second<v)
i++;
if(i==n)break;
ans++,u=p[i].second,v=p[i].first;
}
printf("%d\n",ans);
return 0;
}