[Explanations] Cut the Sequence (greedy interval coverage)

[Explanations] Cut the Sequence (greedy interval coverage)

POJ - 3017

Meaning of the questions:

A given lot of segments, asked in a minimum number of line segments used to cover the line of continuum 1-x.

answer

Consider such a greedy:

According to first sort the left point, if the left point as the Who who long front. Now sentenced to no solution on the convenience, you can record what prefix max. Then choose now to be minimized.

Record a rightmost point \ (R \) , a way violence is the cycle of violence to determine all segments meets the conditions, this is clearly a timeout, you decide to optimize it constant, so you can record it which segment starts from only \ (l_i \ ge R \) . You think you are a constant optimization, complexity fact, you're right ( \ (O (the n-) \) ) ...

      const int QAQ=2020;
      while(QAQ){
        register int l=r+1;
        for(register int t=can;t<=n;++t){
          if(data[t].l<=l&&data[t].r>=l)
            r=max(r,data[t].r);
          if(data[t].l>r) {can=t;break;}
        }
        if(l>r)break;
        else if(++ans,r>=T) break;
      }

Analyze this complexity, above all, can surely would have been larger and larger \ (O (n) \) the cost is \ (O (n) \) , so that \ (O (n) \) of . You might say that out of the for loop is not necessarily due to the break, but if it is not jumping out of the break, then there are no more legal description of the program.

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;  typedef long long ll;
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(c<48||c>57)f|=c==45,c=getchar();
      while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}
int n,T,ans,top,r;
struct LINE{
      int l,r;
      LINE(){l=r=0;}
      LINE(const int&a,const int&b){l=a;r=b;}
}data[25005];

inline bool cmp(const LINE&a,const LINE&b){
      if(a.l!=b.l) return a.l<b.l;
      return a.r>b.r;
}

queue < int > q;
int main(){
#ifndef ONLINE_JUDGE
      freopen("in.in","r",stdin);
      //freopen("out.out","w",stdout);
#endif
      int n=qr();T=qr();
      for(register int t=1,t1,t2;t<=n;++t){
        t1=qr();t2=qr();
        data[t]=LINE(t1,t2);
      }
      sort(data+1,data+n+1,cmp);
      while(r<T){
        register int ew=r+1;
        for(register int t=1;t<=n;++t)
          if(data[t].l<=ew&&data[t].r>=ew)
            r=max(r,data[t].r);
          else if(data[t].l>r){
            top=t;
            break;
          }
        if(ew>r)break;
        else ++ans;
      }
      if(r<T) ans=-1;
      cout<<ans<<endl;
      return 0;
}

Guess you like

Origin www.cnblogs.com/winlere/p/10990011.html