链接:Problem - 1160 (hdu.edu.cn)
![](https://i-blog.csdnimg.cn/blog_migrate/af05f6983ec11de6ad140ab947f1e2be.png)
解题思路:
先把老鼠按体重进行排序,就可以降维思考这个问题,使用dp来解决
代码实现:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int dp[1010];
int pre[10010];
struct Mouse{
int weight;
int speed;
int cnt;//计数老鼠的
/*bool operator <(const Mouse &n)const{
if(weight>=n.weight) return true;
else if (weight==n.weight) return speed<n.speed;
else return false;
}大佬写的重载*/
}mouse[10010];
bool cmp(Mouse a,Mouse b){
if(a.weight!=b.weight) return a.weight>b.weight;
else return a.speed<b.speed;
}
int main(){
int w,s,n=0,pos=0;
while(cin>>w>>s){
mouse[n].weight=w;
mouse[n].speed=s;
mouse[n].cnt=n+1;
n++;
}
sort(mouse,mouse+n,cmp);
for(int i=0;i<n;i++) dp[i]=1;
memset(pre,-1,sizeof(pre));
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(mouse[j].weight>mouse[i].weight&&mouse[j].speed<mouse[i].speed){
if(dp[i]<dp[j]+1){
dp[i]=dp[j]+1;
pre[i]=j;
}
}
}
}
int ans=-1;
for(int i=0;i<n;i++){
if(ans<dp[i]){
ans=dp[i];
pos=i;
}
}
printf("%d\n",dp[pos]);
while(pos!=-1){
printf("%d\n",mouse[pos].cnt);
pos=pre[pos];
}
return 0;
}