洛谷P2970 [USACO09DEC]自私的放牧Selfish Grazing

洛谷P2970 [USACO09DEC]自私的放牧Selfish Grazing

一道贪心题

 1 #include <cstdio> 
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <iostream> 
 6 #include <iomanip> 
 7 #include <string> 
 8 #include <algorithm> 
 9 #define For(i,j,k) for(int i=j;i<=k;i++) 
10 #define Dow(i,j,k) for(int i=j;i>=k;i--) 
11 using namespace std ;  
12 
13 const int N = 50011 ; 
14 struct node{
15     int s,t ; 
16 }seg[N];
17 int n,ans ; 
18 int dp[N] ; 
19 
20 inline int read() 
21 {
22     int x = 0 , f = 1 ; 
23     char ch = getchar() ; 
24     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar(); } 
25     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar(); } 
26     return x * f ; 
27 }
28 inline bool cmp(node a,node b) 
29 {
30     if(a.t!=b.t) return a.t < b.t ; 
31     return a.s < b.s ; 
32 }
33 int main() 
34 {
35     n = read() ; 
36     For(i,1,n) seg[i].s = read() , seg[i].t = read() ; 
37     sort(seg+1,seg+n+1,cmp) ; 
38     
39     int id ; 
40     For(i,1,n) 
41        if(i==1 || seg[id].t<=seg[i].s ) ans++,id=i ; 
42     printf("%d\n",ans) ; 
43     return 0 ; 
44 }

 

posted @ 2017-10-02 21:03  third2333  阅读(151)  评论(0编辑  收藏  举报