Poj 2376 greedy range coverage

Source: Internet
Author: User

Poj 2376 greedy range coverage

Question:

Select as few intervals as possible for n intervals to cover 1 ~ T this interval

If output-1 cannot be overwritten

Ideas:

Classic greedy interval coverage

Sort all intervals from small to large to take the interval at the rightmost of the end point.

Code:

 

#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       #include
        
         #include
         
          using namespace std;#define INF 0x3f3f3f3f#define PI acos(-1.0)#define mem(a, b) memset(a, b, sizeof(a))typedef pair
          
            pii;typedef long long LL;//------------------------------const int maxn = 25005;int n, T;struct node{ int s,e; bool operator < (const node nt) const{ return s < nt.s; }}cow[maxn];void init(){ for(int i = 0; i < n; i++){ scanf("%d%d",&cow[i].s,&cow[i].e); } sort(cow, cow + n);}void solve(){ if(cow[0].s > 1){ printf("-1\n"); return; } int start_ = 1, end_ = 1, cnt = 1; for(int i = 0; i < n; i++){ if(cow[i].s <= start_) end_ = max(end_, cow[i].e); else{ cnt++; start_ = end_ + 1; if(cow[i].s <= start_) end_ = max(end_, cow[i].e); else{printf("-1\n"); return; } } if(end_ >= T) break; } if(end_ >= T) printf("%d\n",cnt); else printf("-1\n");}int main(){ scanf("%d%d",&n,&T); init(); solve(); return 0;}
          
         
        
      
     
    
   
  
 

An error occurred when T_T was returned because a condition end> = T... sigh... was rarely judged in the solve function...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.