hdu-1160 LIS记录路径

本文详细介绍了如何在解决最长递增子序列(LIS)问题的同时记录下构成该序列的具体元素。通过实例题目HDU-1160,阐述了利用递归方法记录路径的过程,并给出了完整的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2019暑期集训的第一篇博客

这次写一下LIS记录路径的问题,

当你学会了LIS后有没有想过把路径给记录下来,就是最长递增子序列是有哪几个数字组成的,怎么记录呢,其实很简单,就是用递归的方法,首先你可以每个点的上一个点是谁,意思就是说知道此时这个点的LIS是在哪个点的基础上转变过来的,我们只要开一个数组,记录每个点的上一个点,然后逆序递归输出就行了,下面给一个例题,来详细的说一下LIS记录路径的问题,

HDU-1160

http://acm.hdu.edu.cn/showproblem.php?pid=1160

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23640    Accepted Submission(s): 10521
Special Judge

 

Problem Description

FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

 

 

Input

Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.

 

 

Output

Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that

W[m[1]] < W[m[2]] < ... < W[m[n]]

and

S[m[1]] > S[m[2]] > ... > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

 

 

Sample Input

 

6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900

 

 

Sample Output

 

4 4 5 9 7

 

 

Source

Zhejiang University Training Contest 2001

 这个题的大致意思就是说有若干老鼠,每个老鼠都有体重和奔跑的速度。找到一个最长的老鼠序列,使老鼠的体重递增,速度递减,并输出这些老鼠的编号

大致思路:

首先我们先根据老鼠的一个指标进行排序,根据速度的递减,或许是根据根据体重的递增,不管怎么样都行。

排好后再根据另一点求LIS就行了,并顺便记录路径。

下面给出代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=1e5+7;
int vis[maxn];//存放路径,就是每个点的上一个点是什么,
int a[maxn];//把路径给倒过来方便输出。
int dp[maxn];//记录LIS
struct stu
{
    int w;
    int s;
    int z;
}A[maxn]//    保存数据,要把每个老鼠的位置给记录下来,因为输出的时候输出的是老鼠的初始ID
bool cmp(stu a,stu b)
{
    return a.s>b.s; //对老鼠的速度进行逆序排序
}
int main()
{
    int n=0,w,s;
    while(~scanf("%d %d",&w,&s))
    {
        n++;
        A[n].w=w;
        A[n].s=s;
        A[n].z=n;
    }
    sort(A+1,A+n+1,cmp);
    int ans=-1;
    for(int i=1;i<=n;i++)
        dp[i]=1,vis[i]=i;
    int z=-1;
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=i-1;j++)
        {
            if(A[i].w>A[j].w&&A[i].s!=A[j].s)
            {
                if(dp[j]+1>dp[i]) vis[A[i].z]=A[j].z;//这个点就是记录路径,当你要更改dp这个数组的时候,那么就更新这个vis数组
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
        if(dp[i]>ans) z=A[i].z;
        ans=max(ans,dp[i]);

    }
    printf("%d\n",ans);
    int cnt=0;
    cnt++;
    a[cnt]=z;
    ans--;
    while(ans--)
    {
        cnt++;
        a[cnt]=vis[z];  //用递归的手法把路径给找出来。 
        z=vis[z];
    }

    for(int i=cnt;i>=1;i--)
        printf("%d\n",a[i]);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值