FatMouse's Speed HDU - 1160

FatMouse’s Speed HDU - 1160

题目大意:

题目给出不超过1000组的数据 从1-n编号
每一组包括两个值
一个是重量,一个是速度

要求找出符合下列条件的最长子序列
序列中重量依次增加 速度依次减少

输出他们的编号(可能有多种答案,输出一种即可)

分析:

这个题首先应该先将其中的一个元素排序
然后分析并查找另一个元素的最长子序列
这里我将重量利用sort()进行排序,分析速度

本题还要求输出最后最长子序列的编号 所以将每一个速度是由哪一组速度推过来的 也记录下来

AC代码:

#include <iostream>
#include <algorithm>
using namespace std;
struct Mouse{
	int id;
	int W;
	int S;
}M[1005];

struct dp{
	int data;
	int fir;
}dp[1005];

struct Rule{
	bool operator()(const Mouse& a,const Mouse& b){
		if(a.W!=b.W)
			return a.W<b.W;
		else{
			return a.S>b.S;
		}
	}	
};

int main(){
	int t=0;
	while(cin>>M[t].W>>M[t].S){
		M[t].id=t+1;
		dp[t].data=1;
		t++;
	}
	sort(M,M+t,Rule());
	int MAX=1;
	for(int i=1;i<t;i++){
		for(int j=0;j<i;j++){
			if(M[i].S<M[j].S && M[i].W!=M[j].W){
				if(dp[j].data+1>dp[i].data){
					dp[i].data=dp[j].data+1;
					dp[i].fir=j;
				}
				MAX=max(MAX,dp[i].data);
			}
		}
	}

	int temp;
	cout<<MAX<<endl;
	for(int i=0;i<t;i++){
		if(dp[i].data==MAX){
			temp=i;
			break;
		}
	}
	
	int ans[1005],K=0;
	while(MAX--){
		ans[K++]=M[temp].id;
		temp=dp[temp].fir;
	}
	for(int i=K-1;i>=0;i--)
		cout<<ans[i]<<endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值