一,問題:
1. 給定一個源區間[x,y]和N個無序的目標區間[x1,y1] [x2,y2] ... [xn,yn],判斷源區間[x,y]是不是在目標區間內。
2. 給定一個視窗地區和系統介面上的N個視窗,判斷這個視窗地區是否被已有的視窗覆蓋。
二,解法:
問題一:
先用區間的左邊界值對目標區間進行排序O(nlogn),對排好序的區間進行合并O(n),對每次待尋找的源區間,用二分查出其左右兩邊界點分別處於合并後的哪個源區間中O(logn),若屬於同一個源區間則說明其在目標區間中,否則就說明不在。
#include <iostream>#include <algorithm>using namespace std;struct Line{int low, high;bool operator<(const Line &l) const{return low<l.low;}};#define MAXN 10001Line lines[MAXN];// 目標區間int ncnt = 0;// 合并後區間的個數#define N 101Line sl[N];// 待查詢的源區間// 用二分尋找找出key所在的區間,以區間的low作為劃分int GetIndex(int key){int u, v;u = 0; v = ncnt-1;while (u<=v)// u,v可取等號{int m = (u+v)>>1;if (key >= lines[m].low)u = m+1;elsev = m-1;}return v;}int main(){int n, k, i, j;cin >> n >> k;// n是目標區間的個數,k是待查詢的源區間的個數for (i=0; i<n; i++)cin >> lines[i].low >> lines[i].high;for (i=0; i<k; i++)cin >> sl[i].low >> sl[i].high;// 排序O(nlogn)sort(lines, lines+n);// 合并O(n)int lasthigh = lines[0].high;for (i=1; i<n; i++)if (lasthigh >= lines[i].low)lasthigh = lines[i].high;else{lines[ncnt++].high = lasthigh;lines[ncnt].low = lines[i].low;lasthigh = lines[i].high;}lines[ncnt++].high = lasthigh;for (i=0; i<k; i++){// 單詞尋找時間O(logn)int s1 = GetIndex(sl[i].low);int s2 = GetIndex(sl[i].high);if (s1==s2 && sl[i].high <= lines[s2].high)printf("Yes\n");elseprintf("No\n");}}
法二:使用並查集,對每個區間合并到一個子樹上,最後判斷源區間的x和y的根是否相同。
#include<iostream>using namespace std;const int size = 100;int father[size];int rank[size];void make_set(int n){ for(int i = 1; i <= n; i ++){ father[i] = i; rank[i] = 1; } }int find_set(int x)//尋找代表元素 { if(x != father[x]){ //元素不是單獨的段,在某個區間內,返回某個區間代表 father[x] = find_set(father[x]); } return father[x];}void Union(int x, int y){ x = find_set(x); y = find_set(y); if(x == y){ //兩個在同一個區間 return ; } if(rank[x] < rank[y]){ father[x] = y; } else{ father[y] = x; if(rank[x] == rank[y]){ rank[x] ++; } }}int main(){ int x1, y1; cin >> x1 >> y1;//輸入要判斷區間 int x, y; int n; cin >> n; //區間的個數 make_set(size); while(n --){ cin >> x >> y; //輸入每個區間 if(x > y){//這一步很關鍵,表示考慮的周到 swap(x, y); } for(int i = x + 1; i <= y; i++){//將區間內的 段合并到已有區間 Union(x, i); } } if(find_set(x1) == find_set(y1)){ cout << "yes" << endl; } else{ cout << "no" << endl; } system("pause");}
(1,3)結合後 father[1-3]=1 rank[1]=2;其餘為1
(2,4)結合後 fahter[1-4]=1 rank[1]=2 ;其餘為1
說明:(1,4)為整個區間,代表為1
法三:將無序的目標區間排序後,再合并成幾個有序的區間,然後把源區間和有序的目標區間比較。
#include<iostream> #include<vector> using namespace std; typedef pair<int, int> section; bool cmp(section a, section b) { return a.first < b.first; } int main() { section src, tmp; cin >> src.first >> src.second; //要尋找的 vector<section> v; while(cin >> tmp.first >> tmp.second, tmp.first | tmp.second){ v.push_back(tmp); } sort(v.begin(), v.end(), cmp);//按第一個域,從小到大排序 vector<section> res; vector<section>::iterator it = v.begin(); int begin = it->first;//記錄區間的開始部分 int end = it->second;//記錄區間的開始部分 if((it+1)==v.end()) //如果只有一個區間 res.push_back(make_pair(begin, end)); else { it ++; for(; it != v.end(); it++){ if(end <= it->first){ //如果第一個pair 的第二個元素,小於下一個pair 的第一個元素。 res.push_back(make_pair(begin, end)); //插入第一個區間 begin = it->first; end = it->second; } else if( (end > it->first) && (end <=it->second)) { //res.push_back(make_pair(begin, end); //插入第一個區間 //begin = it->first; end = it ->second; if((it+1)==v.end()) res.push_back(make_pair(begin, end)); } } } bool solve = false; it = res.begin(); for(; it != res.end(); it ++){ if(src.first >= it->first && src.second <= it->second){ solve = true; break; } } if(solve){ cout << "in" << endl; } else{ cout << "out" << endl; } system("pause"); }
問題二解法:
這個問題適合使用線段樹來解答,單次尋找的時間複雜度為O(nlogn),當然也能用數組解答,但單次尋找的時間複雜度會增加到O(n^2)。這裡我們直接使用線段樹來解答。
線段樹是一棵二叉樹,將數軸劃分成一系列的初等區間[I, I+1] (I=1,2,..,N-1)。每個初等區間對應於線段樹的一個葉結點。線段樹的內部結點對應於形如[ I, J ](J – I > 1)的一般區間。由於線段樹給每一個區間都分配了結點,利用線段樹可以求區間並後的總長度與區間並後的線段數。先給出測試資料(前4行是系統介面上已有的N個視窗,之後的一行是待測試的視窗地區),後面是代碼:
4
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
#include <iostream>#include <cmath>#include <algorithm>using namespace std;// 線段樹的結點struct SegNode{ int low, high;// 線段的兩端點索引 int ncover;// 線段被覆蓋的次數 SegNode *left;// 結點的左子樹 SegNode *right;// 結點的右子樹 SegNode() {low=high=0;ncover=0;left=right=NULL;}};// 構造線段樹,它是一個完全二叉樹void BuildSegTree(SegNode *&tree, int *index, int low, int high){ if (low < high) { tree = new SegNode; tree->low = low; tree->high = high; if (high-low>1) { int m = (low+high)/2; BuildSegTree(tree->left, index, low, m); BuildSegTree(tree->right, index, m, high); } }}// 往線段樹中插入線段,即用線段(low,high)來覆蓋線段樹void InsertSegTree(SegNode *tree, int low, int high){ // 先序遍曆 if (low<=tree->low && tree->high<=high) tree->ncover++; else if (tree->high-tree->low > 1) { int m = (tree->low+tree->high)/2; if (low < m) InsertSegTree(tree->left, low, high); if (m < high) InsertSegTree(tree->right, low, high); }}// 從線段樹中刪除線段void DeleteSegTree(SegNode *tree, int low, int high){ if (low<=tree->low && tree->high<=high) tree->ncover--; else if (tree->high-tree->low > 1) { int m = (tree->low+tree->high)/2; if (low < m) DeleteSegTree(tree->left, low, high); if (m < high) DeleteSegTree(tree->right, low, high); }}// 線段樹中是否包含線段(low,high)bool FindSegTree(SegNode *tree, int low, int high){// 若當前區間被覆蓋,且線段(low,high)屬於當前區間則返回覆蓋if (tree->ncover && tree->low <= low && high <= tree->high )return true;// 若(low,high)沒被當前區間覆蓋,則將其分為兩段,// 分別考慮是否被子結點表示的區間覆蓋else if (tree->high - tree->low > 1){int m = (tree->low + tree->high) >> 1;bool ret = true;if (low<m) ret = FindSegTree(tree->left, low, high<m?high:m);if (!ret) return false;if (m<high) ret = FindSegTree(tree->right, m<low?low:m, high);if (!ret) return false;return true;}return false;}#define LEFT true#define RIGHT false#define INF 10000// 表示豎直方向的線段struct Line{ int starty, endy;// 豎線的長度 int x;// 豎線的位置 bool inout;// 豎線是長方形的左邊還是右邊 bool operator<(const Line& a) const{// 依據x座標進行排序 return x<a.x; }};// 所有豎直方向的線段Line lines[INF];// 對橫向超元線段進行分組int index[INF];int nCnt = 0;// 擷取key的位置int GetIndex(int key){// 用二分尋找查出key在index中的位置return lower_bound(index,index+nCnt,key)-index;}// 擷取key的位置或比它小的最大數的位置int GetLower(int key){size_t pos = lower_bound(index,index+nCnt,key)-index;if (key == index[pos]) return pos;else return pos-1;}// 擷取key的位置或比它大的最小數的位置int GetUpper(int key){return lower_bound(index,index+nCnt,key)-index;}int main(){ int nRec; cin >> nRec; int i, j; int x[2], y[2]; // 讀取nRec個視窗的資料 for (i=0; i<nRec; i++) { cin >> x[0] >> y[0] >> x[1] >> y[1];// 記錄每個長方形的兩條豎直邊 lines[2*i].x=x[0];lines[2*i+1].x=x[1]; lines[2*i].starty=lines[2*i+1].starty=min(y[0],y[1]); lines[2*i].endy=lines[2*i+1].endy=max(y[0],y[1]); lines[2*i].inout=LEFT;lines[2*i+1].inout=RIGHT;// 對豎直的線段進行離散化 index[2*i]=y[0];index[2*i+1]=y[1]; } // 待查詢的視窗地區 Line search[2]; cin >> x[0] >> y[0] >> x[1] >> y[1]; search[0].x=x[0];search[1].x=x[1]; search[0].starty=search[1].starty=min(y[0],y[1]); search[0].endy=search[1].endy=max(y[0],y[1]); search[0].inout=LEFT;search[1].inout=RIGHT; // 對x座標進行排序O(nlogn) sort(index, index+2*nRec); sort(lines, lines+2*nRec); // 排除index數組中的重複資料O(n) for (i=1; i<2*nRec; i++) if (index[i]!=index[i-1]) index[nCnt++] = index[i-1]; index[nCnt++] = index[2*nRec-1]; // 建立線段樹 SegNode *tree; BuildSegTree(tree, index, 0, nCnt-1); // 單詞尋找的時間複雜度為O(nlogn) bool res; InsertSegTree(tree, GetIndex(lines[0].starty), GetIndex(lines[0].endy)); for (i=1; i<2*nRec; i++) { if (lines[i].inout==LEFT)// 遇視窗的左邊界,將其加入線段樹 InsertSegTree(tree, GetIndex(lines[i].starty), GetIndex(lines[i].endy)); else// 遇視窗的右邊界,將其刪出線段樹 DeleteSegTree(tree, GetIndex(lines[i].starty), GetIndex(lines[i].endy));if (lines[i].x!=lines[i-1].x && search[0].x < lines[i+1].x && search[1].x > lines[i].x){// 從待查視窗地區的左邊界開始查詢直到其右邊界結束查詢res = FindSegTree(tree, GetLower(search[0].starty), GetUpper(search[0].endy));if (!res) break;}else if (search[1].x <= lines[i].x)break; } if (res) printf("Yes\n"); elseprintf("No\n"); return 0;}