2012寒假POJ(1)

 寒假开始了,今天上论坛看到算法版的大牛放出了一些对2011级新生的练习题;想来自己也算是2011级新生啊,当年看见人家搞ACM的牛逼的不行,自己到现在还是小菜一只,没办法厚着脸皮做一下吧;编程能力真是弱爆了,还好这个博客知道和关注的人也没几个,就在这记录一下吧。

排序POJ1007:

DNA Sorting
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 61106   Accepted: 24145

 

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. 

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

Source

East Central North America 1998
 
我的思路:题目是根据逆序数的大小来确定对应字符串的顺序,所以问题就转变为两部分:1.求逆序数 2.对应字符串的排序
 
实现方法:使用了C++里面的vector来存储数据,每一个元素都是一个结构体,包含字符串和它所对应的逆序数两部分,排序使用sort函数,在默认升序排序的基础上增加比较规则参数,最后直接根据排好的顺序输出字符串。
 
运行结果:AC。
 
总结:虽然在OJ上通过了,但是发现性能上不是很好,内存占用200多K,时间也花费了200ms.点开Status发现大牛的程序都是OK,1ms,相比之下我的结果真是惨不忍睹;于是在网上搜到了结题报告,果然厉害,很巧妙的解决了找到对应下标的问题:
 

 
差距很大啊,网上还有其他的方法,有兴趣的话还可以看看,不过这里用到的下标保存的方法值得学习,避免了不必要的空间开销;还有就是关于C++的STL了解的还是太少,很多好用的东西都不知道,革命尚未成功,同志仍需努力啊~

你可能感兴趣的:(poj)