J - FatMouse's Speed

DP的题写得多了慢慢也有了思路,虽然也还只是很简单的DP

因为需要输出所有选择的老鼠,所以刚开始的时候想利用状态压缩来储存所选择的老鼠,后面才发现n太大1<<1000根本存不下来...

思路的话其实也不难,把体重排序之后,对速度求一个最长下降子序列即可。

对于每一次求最长有序子序列,只需要全部遍历一遍,遍历的时候,将该位置作为所选择序列的最后一个元素,DP[i]即为该序列的最大长度。

代码:

// Created by CAD on 2019/10/29.
#include <bits/stdc++.h>
using namespace std;

struct state{
    int pre=-1;
    int cnt=1;
}dp[1005];
struct mouse{
    int speed,weight;
    int id;
    bool operator<(mouse &m)
    {
        if(weight!=m.weight)
            return weight<m.weight;
        else return speed>m.speed;
    }
}m[1005];
void print(int n)
{
    if(n==-1) return ;
    print(dp[n].pre);
    cout<<m[n].id<<endl;
}
int main()
{
//    FOPEN;
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n=0,ans=0;
    while(cin >> m[n].weight >> m[n].speed)
    {
        m[n].id=n+1;
        n++;
    }
    sort(m, m+n);
    for(int i=0; i<n; i++)
    {
        for(int k=0; k<i; ++k)
        {
            if(m[k].weight<m[i].weight && m[k].speed>m[i].speed&&dp[k].cnt+1>dp[i].cnt)
                    dp[i].cnt=dp[k].cnt+1, dp[i].pre=k;
        }
        if(dp[ans].cnt<dp[i].cnt)
            ans=i;
    }
    cout<<dp[ans].cnt<<endl;
    print(ans);
    return 0;
}