HDU1379-逆序数排序
来源:互联网 发布:网店数据化分析的内容 编辑:程序博客网 时间:2024/04/30 13:58
DNA Sorting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1716 Accepted Submission(s): 823
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.
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
110 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT
Sample Output
CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA
Source
East Central North America 1998
思路:
题目意思是:根据DNA串的逆序数时行排序,如果DNA串的逆序数相等,则保持DNA原本的相对位置不变。
所以,重点是求出字符串的逆序数,求逆序数可以用蛮力法-O(n^2),也可以用归并排序的方法-O(nlgn)。打开链接
#include <cstdio>#include <string>#include <iostream>#include <algorithm>using namespace std;char arr[120],tmp[120];int sum = 0;struct node{int id;int inversion;char str[120];} dna[120];bool cmp(const node& x,const node& y){if (x.inversion != y.inversion)return x.inversion < y.inversion;elsereturn x.id < y.id;}void Merge(int left,int mid,int right){int i=left, j=mid+1, k=left;while(i <= mid && j <= right){if(arr[i] <= arr[j])tmp[k++]=arr[i++];else{tmp[k++]=arr[j++];sum += mid-i+1;//求逆序数}}while(i <= mid)tmp[k++]=arr[i++];while(j <= right)tmp[k++]=arr[j++];for(int i=left;i <= right;++i)arr[i]=tmp[i];}void MergeSort(int left,int right){if(left < right){int mid = (left + right) >> 1;MergeSort(left,mid);MergeSort(mid+1,right);Merge(left,mid,right);}}int main(){#ifndef ONLINE_JUDGEfreopen("2.txt","r",stdin);#endifint Case,n,m,i;scanf("%d ",&Case);while(Case--){scanf("%d%d ",&n,&m);for (i=0; i < m; ++i){scanf("%s ",arr);strcpy(dna[i].str,arr);sum = 0;MergeSort(0,n-1);dna[i].id = i;dna[i].inversion = sum;}sort(dna,dna+m,cmp);for(i=0; i < m; ++i)puts(dna[i].str);} return 0;}
0 0
- HDU1379-逆序数排序
- 排序之逆序数
- 归并排序,逆序数
- 逆序数&排序
- 归并排序 逆序数
- 归并排序+逆序数
- 归并排序 + 逆序数
- 归并排序,求逆序数
- 归并排序求逆序数
- 逆序数--归并排序-nyoj117
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- C#中Cookie,Session,Application的用法与区别?
- Mongodb当limit写负数去请求数据的时候会发生什么?
- Create Database With DBCA on 11.2.0.3, Fails with ORA-29516 (Doc ID 1450281.1)
- linux 重启/关机命令
- [译]Apache MINA 2.0 第一章 入门指南
- HDU1379-逆序数排序
- Tomcat5发布项目问题(1):jstl java.lang.NoClassDefFoundError javaxelValueExpression
- linux下重启oracle 命令
- [译]Apache MINA 2.0 第二章 基础知识
- 【故障处理】ORA-12162: TNS:net service name is incorrectly specified
- Sql plus命令报command not found的解决笔记
- 老杨的相关博文索引
- C#.net cookie、session实现自动登陆
- Tomcat5发布项目问题(2):默认不解析EL表达式