HDU 1160 FatMouse's Speed (the longest ascending sequence of recorded paths)

meaning of the title

Many fat mice think that the longer they get, the faster they run. To refute this, you now need to study the weight and speed of the mice. You need to find a subsequence in the mouse sequence that makes the mice weigh more, but at a slower rate.
Many fat mice think that the longer they get, the faster they run. To refute this, you now need to study the weight and speed of the mice. You need to find a subsequence in the mouse sequence that makes the mice weigh more, but at a slower rate.
Input
The input ends with eof.
Each row of the input has two positive integers representing the weight and speed of the mice, ranging from 1 to 10,000, with a maximum of 1,000 mice.
Some mice may have the same weight, some may have the same speed, and some may have the same weight and speed.
Output

We wanted to find the longest subsequence in the original mouse sequence so that the weight of the mice in this subsequence increased but the speed decreased.
First output the length of the longest subsequence that meets the criteria.
Secondly, the scheme of outputting a longest subsequence requires the number of each mouse at the time of input, each number occupies one line, and any correct method will be judged to be correct.

Solving problems

First, the mice were sorted by weight ascending and weight descending at the same speed.
Then the longest ascending subsequence of the sorted sequence is obtained under the conditions of weight gain and speed decrease.Since the path is to be recorded, a structure is opened to store the LIS length of the sequence ending with the current mouse and the number of the previous mouse.
See other codes.

AC Code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

const int maxn=1e3+7;
struct node
{
    int w,v,num;
    node(){}
    node(int w,int v,int num):w(w),v(v),num(num){}
    bool operator<(const node &o)const{
        return w<o.w || w==o.w&&v>o.v;
    }
}a[maxn];
struct kode
{
    int pre;
    int sz;
}dp[maxn];

int main()
{
    int w,v,cnt=0;
    while(~scanf("%d%d",&w,&v))
    {
        a[++cnt]=node(w,v,cnt);
    }
    sort(a+1,a+1+cnt);
    int ans=0,len=0;
    for(int i=1;i<=cnt;i++)
    {
        dp[i].sz=1;
        dp[i].pre=0;
        for(int j=1;j<i;j++)
        {
            if(a[j].w<a[i].w && a[j].v>a[i].v)
            {
                if(dp[i].sz<dp[j].sz+1)
                {
                    dp[i].sz=dp[j].sz+1;
                    dp[i].pre=j;
                }
            }
        }
        if(dp[i].sz>len)
        {
            len=dp[i].sz;
            ans=i;
        }
    }
    int tmp=ans;
    printf("%d\n",len);
    stack<int> sta;
    while(tmp)
    {
        sta.push(tmp);
        tmp=dp[tmp].pre;
    }
    while(!sta.empty())
    {
        printf("%d\n",a[sta.top()].num);
        sta.pop();
    }
    return 0;
}

Added by LHBraun on Mon, 03 Feb 2020 19:58:06 +0200