Ural 1303 Minimal Coverage(贪心)

2015-01-27 14:12:28 · 作者: · 浏览: 32

题目地址:Ural 1303

先按每个线段的左端点排序,然后设置一个起点s,每次都从起点小于等于s的线段中找到一个右端点最大的。并将该右端点作为新的起点s,然后继续找。从左到右扫描一遍即可。

代码如下:

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
           #include 
           
             #include 
            
              using namespace std; #define LL __int64 const int INF=0x3f3f3f3f; int a[100000]; struct node { int l, r, ll, rr; } fei[100000]; int cmp(node x, node y) { return x.l
             
              =m) continue ; fei[cnt].ll=ll; fei[cnt].rr=rr; l=ll;r=rr; if(l<0) l=0; if(r>m) r=m; fei[cnt].l=l; fei[cnt++].r=r; } sort(fei,fei+cnt,cmp); s=0; tot=0; if(cnt==0) { printf("No solution\n"); } else { for(i=0; i