HDU - 1160 FatMouse's Speed

input:

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

  

output:

4
4
5
9
7

  

题目大意:

有cnt组数据,每组有体重和速度两个值,求其最长上升子序列并打印出他的路径。

  

分析:

LIS。先将数组按照体重从小到大,速度从大到小排序,之后就是LIS。在判断dp[i]<dp[j]+1是并记录下路径。

  

code:

#define frp

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const int maxn = 1000;
const int MAXN = 10000;
int dp[MAXN];
int pre[MAXN];
struct node{
    int weight,speed,num;
    bool operator <(const node &a)const{
        return weight==a.weight?speed>a.speed:weight<a.weight;
    }
    void pf(){
        cout<<this->weight<<" "<<this->speed<<endl;
    }
}mice[MAXN];

void print(int x){
    if(x==-1)return;
    print(pre[x]);
    cout<<mice[x].num<<endl;
}
void solve() {
    int speed,weight,cnt=0;
    while(cin>>weight>>speed){
        mice[cnt++]={weight,speed,cnt};
    }
    sort(mice,mice+cnt);
    memset(pre,-1,sizeof(pre));
    int ans=-1,last;
    for(int i=0;i<cnt;i++){
        dp[i]=1;
        for(int j=0;j<i;j++){
            if(mice[i].speed<mice[j].speed&&mice[i].weight>mice[j].weight&&dp[i]<dp[j]+1){
                pre[i]=j;
                dp[i]=dp[j]+1;
            }
        }
//        cout<<dp[i]<<endl;
        if(ans<dp[i]){
            ans=dp[i];
            last=i;
        }
    }
    cout<<ans<<endl;
    print(last);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef frp
    freopen("D:\\coding\\c_coding\\in.txt", "r", stdin);
//    freopen("D:\\coding\\c_coding\\out.txt", "w", stdout);
#endif
    solve();
    return 0;
}

  

转载于:https://www.cnblogs.com/visualVK/p/9695758.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值