题干:

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. 

Input

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

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.

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

题目大意:

输入n个长度为m的DNA序列,把他们按照逆序数从小到大稳定排序输出。定义“稳定排序”就是当序列中出现A1==A2时,排序前后A1与A2的相对位置不发生改变。(n<=50 , m<=100)

解题报告:

     跟这题差不多。​​【HihoCoder - 1550】顺序三元组​​。就是找到枚举每一个关键元素然后求和就行。

(好像正解是分治?)

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char maze[105][55];
int num[105];
int a[105],c[105],g[105],t[105];
struct Node {
int val,pos;
Node(){}
Node(int val,int pos):val(val),pos(pos){}
} node[105];
bool cmp(const Node & a, const Node & b) {
return a.val < b.val;
}
int main()
{
int n,m;
cin>>m>>n;//n行m列
for(int i = 1; i<=n; i++) {
scanf("%s",maze[i]+1);
}
for(int i = 1; i<=n; i++) {
for(int j = 1; j<=m; j++) {
if(maze[i][j] == 'A') {
a[i]++;
num[i] += c[i] + g[i] + t[i];
}
else if(maze[i][j] == 'C') {
c[i]++;
num[i] += g[i] + t[i];
}
else if(maze[i][j] == 'G') {
g[i]++;
num[i] += t[i];
}
else t[i]++;
}
}
// for(int i = 1; i<=n; i++) printf("%d\n",num[i]);
for(int i = 1; i<=n; i++) {
node[i] = Node(num[i],i);
}
sort(node+1,node+n+1,cmp);
// for(int i = 1; i<=n; i++) {
// printf("%d %d\n",node[i].pos,node[i].val);
// }
for(int i = 1; i<=n; i++) {
printf("%s\n",maze[node[i].pos] + 1);
}

return 0 ;
}

这题用逆序数也可以搞一发?回头试试。类似HDU - 5775和​​OpenJ_Bailian - 2299​