【拓扑排序】MicroRNA Ranking

2023-11-01 10:30
文章标签 排序 拓扑 ranking microrna

本文主要是介绍【拓扑排序】MicroRNA Ranking,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述
Ahlaam is a computer science student, doing her master thesis on a bioinformatics project about MicroRNAs, special molecule types found in cells. During her thesis, she wants to find microRNAs relevant to a specific health factor in human beings.
Ahlaam has designed k microRNA ranking algorithms, each of which ranks microRNAs from a specific point of view.
There are n microRNAs numbered 1 through n, and each algorithm produces one permutation of these n microRNAs. In the permutation produced by each algorithm, the first microRNA is inferred by the algorithm as the most relevant one to the health factor, and the last microRNA is inferred as the least relevant one. 
Ahlaam wants to report a consensus ranking on microRNAs. In a consensus ranking, if microRNA i is ranked before another mircroRNA j, then at least half of the algorithms should have ranked i before j. Write a program to help Ahlaam find a consensus ranking.

 

输入
There are multiple test cases in the input. The first line of each test contains two space-separated integers n (1 ⩽ n ⩽ 1000) and k (1 ⩽ k ⩽ 200), the number of microRNAs and the number of ranking algorithms, respectively. Then, there are k lines, where the i-th line contains a permutation of n numbers 1, . . . , n, representing the output of the i-th ranking algorithm. The input terminates with a line containing 0 0 which should not be processed.

 

输出
For each test case, print a single line containing a permutation of n numbers 1, . . . , n, representing a possible consensus ranking. If there are more than one correct consensus rankings, print the first one in lexicographic order (a sequence a1 , . . . , an is lexicographically less than a sequence b1 , . . . , bn iff there exists a positive integer j such that ai = bi for all 1 ⩽ i ⩽ j   1 and aj < bj ) . If no such a ranking exists, write “No solution” instead.

 

样例输入
5 3
3 2 4 1 5
4 1 5 2 3
2 4 5 1 3
5 2
5 4 3 2 1
1 2 3 4 5
4 3
1 4 2 3
4 2 3 1
3 1 2 4
0 0

 

样例输出
2 4 1 5 3
1 2 3 4 5
No solution
思路:

  首先,求出各个数的相对位置,然后将相对位置如dp[i][j],理解为i是j的前置,显然就是拓扑排序,但是我训练的数组开小了,不明白啊,我明明特意开大了呀,又坑队友了

代码如下:
  1 #include <iostream>
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 const int maxn = 1e3+50;
  5 vector<int> vec[maxn];
  6 int n,k,num=0;
  7 int indeg[maxn];
  8 int dp[250][1050];
  9 int a[1050];
 10 int b[1050];
 11 priority_queue<int,vector<int>,greater<int> > q;
 12 void init()
 13 {
 14     memset(dp,0,sizeof(dp));
 15     memset(indeg,0,sizeof(indeg));
 16     for(int i=1;i<=n;i++)
 17     {
 18         vec[i].clear();
 19     }
 20 }
 21 void build()
 22 {
 23     for(int i=1;i<=n;i++)
 24     {
 25         for(int j=i+1;j<=n;j++)
 26         {
 27             if(dp[i][j]>dp[j][i])
 28             {
 29                 vec[i].push_back(j);
 30                 indeg[j]++;
 31             }
 32             if(dp[j][i]>dp[i][j])
 33             {
 34                 vec[j].push_back(i);
 35                 indeg[i]++;
 36             }
 37         }
 38     }
 39 }
 40 bool topsort()
 41 {
 42     while(!q.empty())
 43         q.pop();
 44     num=0;
 45     for(int i=1;i<=n;i++){
 46         if(!indeg[i]){
 47             q.push(i);
 48         }
 49     }
 50     while(!q.empty())
 51     {
 52         int now=q.top();
 53         q.pop();
 54         b[num]=now;
 55         num++;
 56         for(int i=0;i<vec[now].size();i++)
 57         {
 58             indeg[vec[now][i]]--;
 59             if(indeg[vec[now][i]]==0)
 60                 q.push(vec[now][i]);
 61         }
 62     }
 63     if(num==n)
 64         return true;
 65     else
 66         return false;
 67 }
 68 void judge()
 69 {
 70     if(topsort())
 71     {
 72         for(int i=0;i<n;i++)
 73         {
 74             if(i)
 75                 printf(" ");
 76             printf("%d",b[i]);
 77         }
 78         printf("\n");
 79     }
 80     else
 81     {
 82         printf("No solution\n");
 83     }
 84 }
 85 int main()
 86 {
 87     while(scanf("%d %d",&n,&k)!=EOF&&n&&k)
 88     {
 89         init();
 90         for(int i=1;i<=k;i++)
 91         {
 92             for(int j=1;j<=n;j++)
 93             {
 94                 scanf("%d",&a[j]);
 95             }
 96             for(int j=1;j<=n;j++)
 97             {
 98                 for(int q=j+1;q<=n;q++)
 99                 {
100                     dp[a[j]][a[q]]++;
101                 }
102             }
103         }
104         build();
105         judge();
106     }
107     return 0;
108 }
View Code

 

转载于:https://www.cnblogs.com/SoulSecret/p/9601501.html

这篇关于【拓扑排序】MicroRNA Ranking的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/322309

相关文章

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

大数据小内存排序问题如何巧妙解决

《大数据小内存排序问题如何巧妙解决》文章介绍了大数据小内存排序的三种方法:数据库排序、分治法和位图法,数据库排序简单但速度慢,对设备要求高;分治法高效但实现复杂;位图法可读性差,但存储空间受限... 目录三种方法:方法概要数据库排序(http://www.chinasem.cn对数据库设备要求较高)分治法(常

Python中lambda排序的六种方法

《Python中lambda排序的六种方法》本文主要介绍了Python中使用lambda函数进行排序的六种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1.对单个变量进行排序2. 对多个变量进行排序3. 降序排列4. 单独降序1.对单个变量进行排序

关于Java内存访问重排序的研究

《关于Java内存访问重排序的研究》文章主要介绍了重排序现象及其在多线程编程中的影响,包括内存可见性问题和Java内存模型中对重排序的规则... 目录什么是重排序重排序图解重排序实验as-if-serial语义内存访问重排序与内存可见性内存访问重排序与Java内存模型重排序示意表内存屏障内存屏障示意表Int

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c