Description
One measure of ``unsortedness'' in a sequence is thenumber 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 isgreater than four letters to its right and E is greater than one letter to itsright. This measure is called the number of inversions in the sequence. Thesequence ``AACEDGG'' has only one inversion (E and D)---it is nearlysorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as canbe---exactly the reverse of sorted).
You are responsible for cataloguing a sequence of DNA strings (sequencescontaining only the four letters A, C, G, and T). However, you want to catalogthem, 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 integern (0 < n <= 50) giving the length of the strings; and a positive integerm (0 < m <= 100) giving the number of strings. These are followed by mlines, each containing a string of length n.
Output
Output the list of input strings, arranged from ``mostsorted'' to ``least sorted''. Since two strings can be equally sorted, thenoutput them according to the orginal order.
Sample Input
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
Source
EastCentral North America 1998
1007
题目大意:
输入:
第一行包含两个数:一个正整数n(0<n<=50)表示字符串长度,一个正整数m(0<m<=100)表示字符串个数。接下来m行,每行一个长度为n的字符串。
输出:
输出输入字符串列表,按排序程度从好到差。如果逆序数相同,就原来顺序输出。
样例输入:
10
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
样例输出:
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
来源:
北美中东部1998
http://acm.pku.edu.cn/JudgeOnline/problem?id=1007
先暴力求逆序数,虽然想找效率高的方法求,不过脑子笨,于是暴力了,然后写了个快排,好吧,我就是对系统提供的不放心(其实是我总忘记怎么用),然后第一次叫re了,原因是,m和n看反了......
#include <stdio.h>
#include <string.h>
int InversionNumber(char Dnastr[55]);
void qsort_my(int l, int h, char Dna[111][55], int invsN[111]);
int main(void)
{
int m, n;
int k = 0;
char Dna[111][55];
int invsN[111];
// freopen("in.txt", "r", stdin);
scanf("%d%d", &m, &n);
for (int i = 0;i < n;i ++)
{
scanf("%s", Dna[k++]);
invsN[k-1] = InversionNumber(Dna[k-1]);
}
qsort_my(0, n, Dna, invsN);
for (int i = 0;i < n;i ++)
{
printf("%s\n", Dna[i]);
}
}
void qsort_my(int l, int h, char Dna[111][55], int invsN[111])
{
if (h < l + 2) return;
int e = h, p = l;
while(l < h)
{
while(++l < e && invsN[l] <= invsN[p]);
while(--h > p && invsN[h] >= invsN[p]);
if (l < h)
{
int temp = invsN[l];
invsN[l] = invsN[h];
invsN[h] = temp;
char temp_c[55];
strcpy(temp_c, Dna[l]);
strcpy(Dna[l], Dna[h]);
strcpy(Dna[h], temp_c);
}
}
int temp = invsN[h];
invsN[h] = invsN[p];
invsN[p] = temp;
char temp_c[55];
strcpy(temp_c, Dna[h]);
strcpy(Dna[h], Dna[p]);
strcpy(Dna[p], temp_c);
qsort_my(p, h, Dna, invsN);qsort_my(l,e,Dna, invsN);
}
int InversionNumber(char Dnastr[55])
{
int count = 0;
for (int i = 0;i < strlen(Dnastr);i ++)
for (int j = i+1;j < strlen(Dnastr); j++)
{
if (Dnastr[i] > Dnastr[j])
count ++;
}
return count;
}
是的,然后我尝试用归并排序写了一个计算逆序数个数的程序,虽然过了但是比上面暴力的速度慢....果然这道题的数据量太小,要不然就是我在哪里搞错了...>_<
#include <stdio.h>
#include <string.h>
#define MAXINT 65535
#define LENGTH 100
int InversionNumber(char Dnastr[55]);
void qsort_my(int l, int h, char Dna[111][55], int invsN[111]);
void Merge_sort(char a[LENGTH], int p, int r);
void Merge(char a[LENGTH], int p, int q, int r);
int inve_sum = 0;
int main(void)
{
int m, n;
int k = 0;
char Dna[111][55];
char Dna_cpy[55];
int invsN[111];
//freopen("in.txt", "r", stdin);
scanf("%d%d", &m, &n);
for (int i = 0;i < n;i ++)
{
scanf("%s", Dna[k++]);
strcpy(Dna_cpy, Dna[k-1]);
inve_sum = 0;
Merge_sort(Dna_cpy, 0, m-1);
invsN[k-1] = inve_sum;
//invsN[k-1] = InversionNumber(Dna[k-1]);
}
qsort_my(0, n, Dna, invsN);
for (int i = 0;i < n;i ++)
{
printf("%s\n", Dna[i]);
}
}
void qsort_my(int l, int h, char Dna[111][55], int invsN[111])
{
if (h < l + 2) return;
int e = h, p = l;
while(l < h)
{
while(++l < e && invsN[l] <= invsN[p]);
while(--h > p && invsN[h] >= invsN[p]);
if (l < h)
{
int temp = invsN[l];
invsN[l] = invsN[h];
invsN[h] = temp;
char temp_c[55];
strcpy(temp_c, Dna[l]);
strcpy(Dna[l], Dna[h]);
strcpy(Dna[h], temp_c);
}
}
int temp = invsN[h];
invsN[h] = invsN[p];
invsN[p] = temp;
char temp_c[55];
strcpy(temp_c, Dna[h]);
strcpy(Dna[h], Dna[p]);
strcpy(Dna[p], temp_c);
qsort_my(p, h, Dna, invsN);qsort_my(l,e,Dna, invsN);
}
void Merge_sort(char a[LENGTH], int p, int r){
int q;
if (p < r){
q = (p + r) >> 1;
Merge_sort(a, p, q);
Merge_sort(a, q + 1, r);
Merge(a, p, q, r);
}
}
void Merge(char a[LENGTH], int p, int q, int r){
int L[(LENGTH >> 1) + 1], R[(LENGTH >> 1) + 1];
int n1 = 0, n2 = 0;
for (int i = p;i <= q;i ++){
L[n1++] = a[i];
}
L[n1] = MAXINT;
for (int i = q + 1;i <= r;i ++){
R[n2++] = a[i];
}
R[n2] = MAXINT;
n1 = 0, n2 = 0;
for (int k = p;k <= r; k++){
if (L[n1] <= R[n2]){
a[k] = L[n1];
n1++;
}else{
a[k] = R[n2];
inve_sum += q - p + 1 - n1;
n2++;
}
}
}