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
先对END元素按升序SORT
然后动态规划找出最长序列,最后输出最长序列每加一的时候的第一个元素序号
对只有一组元素单独输出 1
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef struct seq
{
int starts;
int ends;
int no;
};
int cmp(seq a,seq b)
{
if(a.ends!=b.ends)
return a.ends<b.ends;
return a.starts<b.starts;
}
int main()
{
int n;
while(cin>>n&&n!=0)
{
seq s[n];
int a[n];
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
cin>>s[i].starts>>s[i].ends;
s[i].no=i+1;
}
sort(s,s+n,cmp);
a[0]=1;
int max=-1;
int maxNo;
for(int i=1;i<n;i++)
{
for(int j=i-1;j>=0;j--)
{
if(s[i].starts>=s[j].ends&&a[i]<=a[j]+1)
{
a[i]=a[j]+1;
}
if(a[i]>=max)
{
max=a[i];
maxNo=i+1;
}
}
}
if(n==1)
cout<<'1'<<endl;
else
{
for(int i=0,j=1;i<n&&j<=max;i++)
{
if(a[i]==j)
{
if(j==1)
cout<<s[i].no;
else
cout<<" "<<s[i].no;
j++;
}
}
cout<<endl;
}
}
return 0;
}