HDU 1379 DNA sorting(求逆序数)

               DNA Sorting

Problem Description

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.

This problem contains multiple test cases!

The
first line of a multiple input is an integer N, then a blank line
followed by N input blocks. Each input block is in the format indicated
in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

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 (1 < 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‘‘. If two or more strings are equally sorted, list them in the
same order they are in the input file.

Sample Input

1

10 6

AACATGAAGG

TTTTGGCCAA

TTTGGCCAAA

GATCAGATTT

CCCGGGGGGA

ATCGATGCAT

Sample Output

CCCGGGGGGA

AACATGAAGG

GATCAGATTT

ATCGATGCAT

TTTTGGCCAA

TTTGGCCAAA

此题傻暴力求逆序数即可,我用的归并排序求。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<malloc.h>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 int ans;
 8
 9 struct str
10 {
11     char s[105];
12     char sort_s[105];
13     int Num;
14 };
15
16 bool cmp(struct str a,struct str b)
17 {
18     return a.Num<b.Num;
19 }
20
21 void MergeArray(char a[],int L,int Mid,int R,char temp[])
22 {
23     int k=0;
24     int i=L,j=Mid+1;
25     int m=Mid,n=R;
26     while(i<=m&&j<=n)
27     {
28         if(a[i]<=a[j])
29         temp[k++]=a[i++];
30         else
31         {
32           temp[k++]=a[j++];
33           ans+=m-i+1;
34         }
35     }
36     while(i<=m)
37     temp[k++]=a[i++];
38     while(j<=n)
39     temp[k++]=a[j++];
40     for(i=0;i<k;i++)
41     a[L+i]=temp[i];
42 }
43
44 void MergeSort(char a[],int L,int R,char temp[])
45 {
46     if(L<R)
47     {
48         int Mid=(L+R)/2;
49         MergeSort(a,L,Mid,temp);
50         MergeSort(a,Mid+1,R,temp);
51         MergeArray(a,L,Mid,R,temp);
52     }
53 }
54
55 int main()
56 {
57     struct str S[55];
58     int i,m,n,t;
59     scanf("%d",&t);
60     while(t--)
61     {
62         scanf("%d%d",&m,&n);
63         for(i=0;i<n;i++)
64         scanf("%s",S[i].s);
65         for(i=0;i<n;i++)
66         {
67             ans=0;
68             int len=strlen(S[i].s);
69             strcpy(S[i].sort_s,S[i].s);
70             char *p=(char*)malloc(sizeof(char)*(len+1));
71             MergeSort(S[i].sort_s,0,len-1,p);
72             S[i].Num=ans;
73             free(p);
74         }
75         sort(S,S+n,cmp);
76         for(i=0;i<n;i++)
77         printf("%s\n",S[i].s);
78     }
79     return 0;
80 } 
时间: 2025-01-12 13:13:45

HDU 1379 DNA sorting(求逆序数)的相关文章

hdu 4911 Inversion(求逆序数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 228 Problem Description bobo has a sequence a1,a2,

HDU 4911 Inversion(归并排序求逆序数)

归并排序求逆序数,然后ans-k与0取一个最大值就可以了. 也可以用树状数组做,比赛的时候可能姿势不对,树状数组wa了.. Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 578    Accepted Submission(s): 249 Problem Description bobo has a seque

hdu 1379 DNA Sorting

DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1924    Accepted Submission(s): 949 Problem Description One measure of ``unsortedness'' in a sequence is the number of pairs of entrie

poj 1007 DNA Sorting (求逆序数)

DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 83069   Accepted: 33428 Description 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 instanc

HDU 4911 Inversion 求逆序数对

点击打开链接 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1106    Accepted Submission(s): 474 Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent num

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个n个数的序列,其中组成的数只有0-n,我们可以进行这么一种操作:把第一个数移到最后一个,次数不限.问,在原始数列和最新生成的数列中逆序数最小可以是多少? 刚开始以为需要枚举求逆序数,但最后知道了这个题是有规律的:一个由0-n组成的n个数的数列,当第一个数移到最后一位的时候,整个数列的逆序数会减少x[i](移动前,后面比他小的),会增加n-x[i]-1(移动后,前面比他大的). 那

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

HDU 1394 Minimum Inversion Number(线段树求逆序数)

题目地址:HDU 1394 这题可以用线段树来求逆序数. 这题的维护信息为每个数是否已经出现.每次输入后,都从该点的值到n-1进行查询,每次发现出现了一个数,由于是从该数的后面开始找的,这个数肯定是比该数大的.那就是一对逆序数,然后逆序数+1.最后求完所有的逆序数之后,剩下的就可以递推出来了.因为假如目前的第一个数是x,那当把他放到最后面的时候,少的逆序数是本来后面比他小的数的个数.多的逆序数就是放到后面后前面比他大的数的个数.因为所有数都是从0到n-1.所以比他小的数就是x,比他大的数就是n-