URAL 1019-Line Painting

Source: Internet
Author: User

Just like a previous question, it is always a problem of interval dyeing. I still use my old method to implement interval discretization + binary interval endpoint + interval processing, and the time is quite short.

 

 

 
 

The problem is that the leftmost end is 0, the rightmost end is 1e9, and the interval is an open interval.

#include <stdio.h>   #include <string.h>   #include <math.h>   #include <algorithm>   using namespace std;  typedef struct  {      int l;      int r;      bool color;  }seg;  seg a[5005];  int t[10005];  int d[10005],ct;  int f[10005];    int find(int x,int n)  {      int l=0,r=n-1,mid=(l+r)/2;      while(l<r)      {          if(x>d[mid])l=mid+1;          else r=mid;          mid=(l+r)/2;      }      return mid;  }    int main()  {      int n;          scanf("%d",&n);          for(int i=0;i<n;i++)          {              char e;              scanf("%d %d %c",&t[2*i],&t[2*i+1],&e);              a[i].l=t[2*i];              a[i].r=t[2*i+1];              a[i].color=(e=='w')?0:1;          }          t[2*n]=0;t[2*n+1]=1e9;          sort(t,t+2*n+2);          ct=0;          d[ct++]=t[0];          for(int i=1;i<2*n+2;i++)              if(t[i]!=t[i-1])              d[ct++]=t[i];          memset(f,0,sizeof(f));          for(int i=0;i<n;i++)          {                 int L=find(a[i].l,ct);                 int R=find(a[i].r,ct);                 for(int j=L;j<R;j++)                  f[j]=a[i].color;          }          int left=0;          int max=0,a1,a2;          for(int i=0;i<ct;i++)          {              if(f[i]==1||d[i]==1e9)              {                  if(left==d[i]){left=d[i+1];continue;}                  else {                          if(d[i]-left>max){                                  max=d[i]-left;                                  a1=left;                                  a2=d[i];                          }                          left=d[i+1];                  }              }          }          printf("%d %d\n",a1,a2);      return 0;  }  

 

Related Article

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.