洛谷P2207 Photo

洛谷P2207 Photo
关于这题 比如奶牛 i 与 奶牛 j 关系不好 强制 i < j
那么我们就将其抽象成 区间 i--j 中必须选择一个点
最少需要选择几个点,然后yy一下可以发现答案其实就相当于就是
最多能选择几段互不干扰的区间,(但我不会严格证明 )然后就可以贪心

按右端点排序 能取就取

其实也不能说是贪心吧,只是模拟而已,一直取完为止

 1 #include <bits/stdc++.h> 
 2 #define For(i,j,k) for(int i=j;i<=k;i++) 
 3 using namespace std ; 
 4 
 5 const int N = 1011 ; 
 6 struct node{
 7     int s,t ; 
 8 }a[N];
 9 int n,K ; 
10 inline int read() 
11 {
12     int x = 0 , f = 1 ; 
13     char ch = getchar() ; 
14     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ; } 
15     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar() ; } 
16     return x * f ;  
17 }
18 
19 inline bool cmp(node a,node b)
20 {
21     if(a.t!=b.t) return a.t < b.t ; 
22     return a.s < b.s ; 
23 }
24 
25 int main() 
26 {
27     K = read() ; n = read() ; 
28     For(i,1,n) {
29         a[i].s = read() , a[i].t = read() ; 
30         if(a[i].s>a[i].t) swap(a[i].s,a[i].t) ; 
31     }
32     sort(a+1,a+n+1,cmp) ; 
33     int last = a[1].t , cnt = 1 ;
34     For(i,2,n) 
35         if(a[i].s>last) cnt++,last=a[i].t ; 
36     printf("%d\n",cnt+1) ; 
37     return 0 ; 
38 }

 

posted @ 2017-10-05 15:46  third2333  阅读(160)  评论(0编辑  收藏  举报