码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷P2207 Photo

时间:2017-10-05 16:10:42      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:swap   getchar   har   --   关系   logs   ret   main   需要   

洛谷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 }

 

洛谷P2207 Photo

标签:swap   getchar   har   --   关系   logs   ret   main   需要   

原文地址:http://www.cnblogs.com/third2333/p/7629300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!