Gene Assembly
Time Limit: 2 Seconds Memory Limit: 65536 KB
Statement of the Problem
With the large amount of genomic DNA sequence data being made available, it is becoming more important to find genes (parts of the genomic DNA which are responsible for the synthesis of proteins) in these sequences. It is known that for eukaryotes (in contrast to prokaryotes) the process is more complicated, because of the presence of junk DNA that interrupts the coding region of genes in the genomic sequence. That is, a gene is composed by several pieces (called exons) of coding regions. It is known that the order of the exons is maintained in the protein synthesis process, but the number of exons and their lengths can be arbitrary.
Most gene finding algorithms have two steps: in the first they search for possible exons; in the second they try to assemble a largest possible gene, by finding a chain with the largest possible number of exons. This chain must obey the order in which the exons appear in the genomic sequence. We say that exon i appears before exon j if the end of i precedes the beginning of j.
The objective of this problem is, given a set of possible exons, to find the chain with the largest possible number of exons that cound be assembled to generate a gene.
Input Format
Several input instances are given. Each instance begins with the number 0 < n < 1000 of possible exons in the sequence. Then, each of the next n lines contains a pair of integer numbers that represent the position in which the exon starts and ends in the genomic sequence. You can suppose that the genomic sequence has at most 50000 basis. The input ends with a line with a single 0.
Output Format
For each input instance your program should print in one line the chain with the largest possible number of exons, by enumerating the exons in the chain. If there is more than one chain with the same number of exons, your program can print anyone of them.
Sample Input
6
340 500
220 470
100 300
880 943
525 556
612 776
3
705 773
124 337
453 665
0
Sample Output
3 1 5 6 4
2 3 1
类似于并查集的问题,首先排序,然后遍历即可
1 #include <iostream> 2 #include <cmath> 3 #include <cstdio> 4 #include <vector> 5 #include <list> 6 #include <string> 7 #include <cstring> 8 #include <cstdio> 9 #include <algorithm> 10 #include <set> 11 12 using namespace std; 13 14 struct Exon 15 { 16 int start, end; 17 int index; 18 }; 19 20 bool cmp(const Exon& e1, const Exon& e2) 21 { 22 if (e1.start == e2.start) 23 return e1.end < e2.end; 24 25 return e1.start < e2.start; 26 } 27 28 int main() 29 { 30 int n; 31 32 while (cin >> n && n) 33 { 34 vector<Exon> vec; 35 for (int i = 0; i < n; ++i) 36 { 37 Exon exon; 38 cin >> exon.start >> exon.end; 39 exon.index = i + 1; 40 vec.push_back(exon); 41 } 42 sort(vec.begin(), vec.end(), cmp); 43 44 vector<int> chains[1005]; 45 int chainsNum = 0; 46 int len[1005], end[1005]; 47 memset(len, 0, 1005 * sizeof(int)); 48 49 for (int i = 0; i < vec.size(); ++i) 50 { 51 Exon exon = vec[i]; 52 53 int j = 0; 54 for (; j < chainsNum; j++) 55 { 56 if (end[j] <= exon.start) 57 { 58 end[j] = exon.end; 59 chains[j].push_back(exon.index); 60 } 61 } 62 if (j == chainsNum) 63 { 64 chains[chainsNum].push_back(exon.index); 65 end[chainsNum] = exon.end; 66 ++chainsNum; 67 } 68 } 69 70 int maxIndex, max = 0; 71 for (int i = 0; i < chainsNum;i++) 72 { 73 if (chains[i].size() > max) 74 { 75 max = chains[i].size(); 76 maxIndex = i; 77 } 78 } 79 80 for (int i = 0; i < chains[maxIndex].size() - 1; i++) 81 { 82 cout << chains[maxIndex][i] << " "; 83 } 84 cout << chains[maxIndex].back() << endl; 85 } 86 }