【归并排序求逆序对】poj1007

DNA Sorting

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
  
求出每个串的逆序对的个数,然后按逆序对个数对DNA串进行排序。
求逆序对的方法是用归并排序
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ty
{
    long x;
    string s;
    inline bool operator < (const ty& b) const
    {
		if (x < b.x) return true;
			else return false;
    }
};
long n, m;
string t;
ty a[200];
long sum;

void merge1(long l, long mid, long r)
{
    char tmp1[200] = {"\0"}, tmp2[200] = {"\0"};

    for (long i = l; i <= mid; i++)
    {
        tmp1[i] = t[i];
    }

    for (long i = mid + 1; i <= r; i++)
    {
        tmp2[i] = t[i];
    }
 
    tmp1[mid + 1] = 'Z';
    tmp2[r + 1] = 'Z';
    long i = l , j = mid + 1;


    for (long k = l; k <= r; k++)
    {
        if (tmp1[i] <= tmp2[j])
        {
            t[k] = tmp1[i];
            i++;
        }
        else{
            t[k] = tmp2[j];
            sum +=mid - i + 1;
            j++;
        }
    }
}
void merge_sort(long l, long r)
{
    if (l <r)
    {
        long mid = (l + r) / 2;
        merge_sort(l, mid);
        merge_sort(mid + 1, r);
        merge1(l, mid, r);
    }

}
long nixudui(long i)
{
    sum = 0;
    t = a[i].s;
    merge_sort(0, m - 1);
    return sum;


}
int main()
{
    freopen("G.in","r",stdin);
    while (scanf("%d%d\n", &m, &n) != EOF)
    {
        for (long i = 1; i<= n; i++)
        {
            cin >> a[i].s;
            a[i].x = nixudui(i);
        }
        sort(a + 1, a + 1 + n);
        for (long i = 1; i<= n; i++)
        {
            cout << a[i].s<<endl;
        }
    }

    return 0;
}

全部评论

相关推荐

想干测开的tomca...:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务