题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76

题目大意:在一个DNA上,给定许多基因的起始位置和结束位置,求出这条链上最多同时存在多少基因?并依次输出选择基因的序列号。

Sample Input

6
340 500
220 470
100 300
880 943
525 556
612 776
3
705 773
124 337
453 665
0

Sample Output

3 1 5 6 4
2 3 1

分析:有两种思路。1.最长上升子序列+路径打印;2.贪心

法1:

 //dp[i] = max{0,dp[j]}+1,j<i
//时间复杂度仍为 O(n^2)
// # include<stdio.h>
# include<string.h>
# include<stdlib.h>
# define MAX struct node{
int x,y,id; //x,y分别是基因起始和结束位置,id表示基因的序列号
}data[MAX]; int n;
int pre[MAX]; //记录前驱
int dp[MAX]; //LIS中保存最大数目
int output[MAX]; //记录输出 int cmp(const void *a,const void *b){ //将序列从小到大排序
struct node *c = (node *)a;
struct node *d = (node *)b;
if(c->x == d->x)
return c->y - d->y;
return c->x - d->x;
} void solve(){
int i,j,m;
qsort(data,n,sizeof(data[]),cmp);
pre[] = -;
dp[] =;
for(i=;i<n;i++){
pre[i] = -;
dp[i] = ;
m = ;
for(j=;j<i;j++){
if(data[j].y < data[i].x){
if(m<dp[j]){
m = dp[j]; //LIS
pre[i] = j;
}
}
}
dp[i] += m ;
}
j = m = ;
for(i=;i<n;i++){ //找到最长的序列的最后一个
if(dp[i] > m){
j = i;
m = dp[i];
}
}
i = ;
while(j != -){
output[i++] = j;
j = pre[j];
}
for(j=i-; j>; j--)
printf("%d ",data[output[j]].id);
printf("%d\n",data[output[j]].id);
}
int main(){
while(scanf("%d",&n) && n){
for(int i=;i<n;i++){
scanf("%d%d",&data[i].x,&data[i].y);
data[i].id = i+;
}
solve();
}
return ;
}

法2:

 /*可以将基因起始点看成一个作业的开始时间,基因终点看成完成该作业的终止时间。用这些基因拼出长度最长的基因序列(连续的基因前一个终点不能大于等于后一个基因起始点),相当于求能完成的最多作业序列。这是贪心的典型例题。可用贪心算法解决,贪心策略——优先考虑先完成的作业。*/
#include <stdio.h>
#include <string.h> int input[][]; //input[i][0]:起始位置,input[i][1]:终止位置,input[i][2]:编号 int main( )
{
int N, i, j, k;
int temp, max;
/*输入基因个数N,N=0时测试结束*/
while( scanf( "%d", &N ) )
{
if( N== ) break; //输入结束
memset( input, , sizeof(input) );
/*输入基因起始点,终止点,并给基因编号*/
for( i=; i<N; i++ )
{
scanf( "%d%d", &input[i][], &input[i][] );
input[i][] = i + ;
}
/*对基因按终止点小的排序,<1000的用例,选择排序法*/
for( i=; i<N; i++ )
{
temp = input[i][];
k = i;
for( j = i + ; j<N; j++ )
{
if( temp>input[j][] )
{
k = j;
temp = input[j][];
}
}
temp = input[i][];
input[i][] = input[k][];
input[k][] = temp;
temp = input[i][];
input[i][] = input[k][];
input[k][] = temp;
temp = input[i][];
input[i][] = input[k][];
input[k][] = temp;
}
/*贪心选择输出被选择的基因编号*/
max = input[][];
printf( "%d", input[][] );
for( i=; i<N; i++ )
{
if( input[i][]>max )
{
printf( " %d", input[i][] );
max = input[i][];
}
}
printf("\n");
}
return ;
}

ZOJ 1076 Gene Assembly(LIS+路径打印 贪心)的更多相关文章

  1. ZOJ 1076 Gene Assembly LIS

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76 题目大意: 题目前面都是废话. 给你一串基因,然后给你上面的外显子的起始和终 ...

  2. ZOJ 1076 Gene Assembly

    原题链接 题目大意:首先学习一个生物学的单词,exon:外显子,DNA序列中能够翻译表达的片段.给出很多外显子的起始点和终点,求寻找包含最多外显子的一条链,并且输出这些外显子的编号. 解法:先把所有外 ...

  3. L2-001 紧急救援 (25 分) (最短路+路径打印)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题目: 作为一个城市的应急救援队伍的负 ...

  4. Minimum Transport Cost HDU1385(路径打印)

    最短路的路径打印问题 同时路径要是最小字典序 字典序用floyd方便很多 学会了两种打印路径的方法!!! #include <stdio.h> #include <string.h& ...

  5. POJ 3984 迷宫问题(简单bfs+路径打印)

    传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. 代码实现:从键盘输入接收一个文件夹路径,打印出该文件夹下所有的.java文件名

    package com.loaderman.test; import java.io.File; import java.io.FileReader; import java.util.Scanner ...

  7. 贪心,Gene Assembly,ZOJ(1076)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76 解题报告: 1.类似活动安排问题. 2.输出格式要注意. #inc ...

  8. HDU-1160.FatMouse'sSpeed.(LIS变形 + 路径打印)

    本题大意:给定一定数量的数对,每个数保存着一只老鼠的质量和速度,让你求出一个最长序列,这个序列按照质量严格递增,速度严格递减排列,让你输出这个序列的最长长度,并且输出组成这个最长长度的序列的对应的老鼠 ...

  9. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

随机推荐

  1. visual studio 资源视图 空白 解决方案

    visual studio 资源视图打开后显示空白的解决方案步骤: 在解决方案view下,右键点击工程 1 unload projects 完成第一步后仍然在解决方案view下,右键点击工程 2 re ...

  2. [leetcode]二分查找总结

    Search for a Range 1.最简单的想法,用最普通的二分查找,找到target,然后向左右扩张,大量的重复的target,就会出现O(n)效率. class Solution { pub ...

  3. Uoj #131. 【NOI2015】品酒大会 后缀数组,并查集

    #131. [NOI2015]品酒大会 统计 描述 提交 自定义测试 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项, ...

  4. JAVA--对象锁

    在并发环境下,解决共享资源冲突问题时,可以考虑使用锁机制. 1.对象的锁 所有对象都自动含有单一的锁. JVM负责跟踪对象被加锁的次数.如果一个对象被解锁,其计数变为0.在任务(线程)第一次给对象加锁 ...

  5. add-apt-repository cloud-archive:liberty

    apt-get update && apt-get upgrade;

  6. linux内核--页高速缓存

    页高速缓存,可以理解为对磁盘中的文件内容进行缓存的一种缓存策略,当然它不仅仅用于磁盘文件. 当对同一磁盘数据反复访问时,缓存数据就是非常必须的了.这就是buffer和 cache这两个概念中的buff ...

  7. spring官方文档中文版

    转 http://blog.csdn.net/tangtong1/article/details/51326887 spring官方文档:http://docs.spring.io/spring/do ...

  8. 微信开发第5章 通过accesstoken获取用户基本信息并修改用户备注

    在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的.对于不同公众号,同一用户的openid不同).公众号可通过本接口来根据Op ...

  9. gitbook 制作 beego 参考手册

    安装gitbook工具 npm install -g gitbook-cli 从github 下载beego文档 https://github.com/beego/beedoc 创建目录 在 zh-c ...

  10. Razor 语法快速参考

    Razor 语法快速参考   本文引自:http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx 语法名称 Raz ...