标签:des style blog http color os 使用 io for
将源区间按照x坐标大小排序,然后进行合并,使用二分查找目标区间x,y坐标在源区间中的分布,若两者分布于同一连续区间,则区间重合
代码:
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 struct section 6 { 7 int low; 8 int high; 9 bool operator<(const section &l)const 10 { 11 return low<l.low; 12 } 13 }; 14 15 int getIndex(int num); 16 17 const int N=1001; 18 section source[N]; 19 const int K=101; 20 section des[K]; 21 //源区间合并后段数 22 int ncnt=0; 23 24 int main() 25 { 26 //n:源区间个数 k:目标区间个数 27 int n,k; 28 cin>>n>>k; 29 for(int i=0;i<n;++i) 30 cin>>source[i].low>>source[i].high; 31 for(int i=0;i<k;++i) 32 cin>>des[i].low>>des[i].high; 33 34 sort(source,source+n); 35 36 int highnum=source[0].high; 37 for(int i=1;i<n;++i) 38 if(source[i].low<=highnum) 39 { 40 //处理[1,6] [2,5]查[2,6]这种情况 41 if(source[i].high<highnum) 42 continue; 43 else 44 highnum=source[i].high; 45 } 46 else 47 { 48 source[ncnt++].high=highnum; 49 source[ncnt].low=source[i].low; 50 highnum=source[i].high; 51 } 52 source[ncnt++].high=highnum; 53 54 for(int i=0;i<k;++i) 55 { 56 int indexlow=getIndex(des[i].low); 57 int indexhigh=getIndex(des[i].high); 58 if(indexlow==indexhigh&&indexlow!=-1&&des[i].high<source[indexhigh].high) 59 cout<<"the "<<i+1<<" section is match"<<endl; 60 else 61 cout<<"the "<<i+1<<" section is not match"<<endl; 62 } 63 64 return 0; 65 } 66 67 int getIndex(int num) 68 { 69 if(num<source[0].low||num>source[ncnt-1].high) 70 return -1; 71 int u=0; 72 int v=ncnt-1; 73 while(u<=v) 74 { 75 int m=(u+v)>>1; 76 if(num>=source[m].low) 77 u=m+1; 78 else 79 v=m-1; 80 } 81 82 return v; 83 }
标签:des style blog http color os 使用 io for
原文地址:http://www.cnblogs.com/chengyuz/p/3924568.html