zoj 1108 - FatMouse's Speed

本文介绍了一种使用动态规划方法解决的问题——寻找体重递增时速度变慢的老鼠的最大集合。通过定义结构体存储老鼠的体重、速度和顺序,利用qsort进行排序,并采用最长不下降子序列算法(LIS)来找到符合条件的最大集合。

题目:给你一些老鼠的体重,和速度,求出体重递增时速度变慢的最大集合。

分析:dp,LIS。最大不下降子序列。

说明:看到这个很久以前写得程序才发现,很多风格都变了 。(2011-09-21 11:34)

#include<iostream>
#include<cstdlib>
using namespace std;

struct mice
{
    int weith;
    int speed;
    int order;
}mouse[ 1001 ];

int length[ 1001 ];
int front[ 1001 ];

int fun( const void *a , const void *b )
{
    mice *p = (mice *)a,*q = (mice *)b;
    return p->weith - q->weith; 
} 

int main()
{
    int t = 0;
    while ( cin >> mouse[ t ].weith >> mouse[ t ].speed )
        mouse[ t ].order = t ++;

    for ( int i = 0 ; i < t ; ++ i )
        length[ i ] = 1;
    qsort( mouse , t , sizeof( mouse[0] ) , fun );
    for ( int i = 1 ; i < t ; ++ i )
    {
        int max = 0;
        for ( int j = 0 ; j < i ; ++ j )
            if  ( mouse[j].speed > mouse[i].speed && length[j] > max ) {
                max = length[j];
                front[ i ] = j ;
            } 
        length[ i ] = max + 1;
    }
    int max_length = 0,max_space = 0;
    for ( int i = 0 ; i < t ; ++ i )
        if  ( length[ i ] > max_length ) {
            max_length = length[ i ];
            max_space  =  i;
        }
        
    int a[1001],con = 0;
    while ( length[ max_space ] > 1 ) {
        a[ con ++ ] = mouse[ max_space ].order;
        max_space   = front[ max_space ];
    }
    a[ con ++ ] = mouse[ max_space ].order;
    
    cout << con << endl;
    for ( int i = con-1 ; i >= 0 ; -- i )
        cout << a[ i ]+1 << endl;
    
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值