总时间限制:
1000ms
内存限制:
65536kB
描述
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted).
You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.
输入
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
输出
Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.
样例输入
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
样例输出
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
来源
East Central North America 1998
1.题解:
计算逆序数,然后根据逆序数,对DNA序列进行排序。
输入m个长度为n的DNA序列,把他们按照逆序数从小到大稳定排序输出。
“稳定排序”就是当序列中出现A1==A2时,排序前后A1与A2的相对位置不发生改变。
在这里不能用简单sort。要stable_sort。因为sort排序如果逆序数相同则不分大小随机排序。
冒泡+ stable _sort==Accepted。(文章出处)见下面链接
2.补充: 这篇文章很赞的 稳定排序与不稳定排序方法
代码非原作。。。。。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <iomanip>
using namespace std;
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
ios::sync_with_stdio(0);
char s[200][200];
int m, n, vis[200], fl = 0;
memset(vis,0,sizeof(vis));
cin>>n >> m;
vis[0] = INF;
for (int i=1;i<=m;i++){
cin>>s[i];
for (int j=0;j<n;j++){
for (int g=j+1;g<n;g++){
if (s[i][j] > s[i][g]){
vis[i]++;
}
}
}
}
for (int i=1;i<=m;i++){
for (int j=1;j<=m;j++){
if (vis[j] < vis[fl]){
fl = j;
}
}
cout<<s[fl]<<endl;
vis[fl] = INF;
}
return 0;
}