前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU1160(LIS)

HDU1160(LIS)

作者头像
全栈程序员站长
发布2022-07-06 10:55:15
1570
发布2022-07-06 10:55:15
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160

题意:求体重下降。速度添加的样例最多有多少个

依据体重降序排一下,然后求速度的最长上升子序列 ,经典的LIS问题,记录一下路径

代码例如以下:

代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1010;

struct cat{
    int w,s,id;
    bool operator < (const struct cat &b) const
    {
        return w>b.w;
    }
}c[maxn];

int q[maxn],path[maxn];

int bin_search(int x,int l,int r)
{
    while(l<=r){
        int mid=(l+r)>>1;
        if(c[q[mid]].s<x) l=mid+1;
        else r=mid-1;
    }
    return l;
}
int main()
{
    //freopen("out.txt","w",stdout);
    int cnt=0;
    while(cin>>c[cnt].w>>c[cnt].s) c[cnt].id=cnt+1,cnt++;
    sort(c,c+cnt);
    //cout<<"**************"<<endl;
    //for(int i=0;i<cnt;i++)
      //  cout<<c[i].w<<" "<<c[i].s<<" "<<c[i].id<<endl;
    //cout<<"**************"<<endl;
    int len=0;
    q[len++]=0;
    path[0]=0;
    for(int i=1;i<cnt;i++){
        if(c[i].s>c[q[len-1]].s){
            path[i]=q[len-1];
            q[len++]=i;
        }
        else{
            int idex = bin_search(c[i].s,0,len-1);
            path[i] = idex ? q[idex-1] : 0;
            q[idex] = i;
        }
    }
    printf("%d\n",len);
    printf("%d\n",c[q[len-1]].id);
    int tmp=q[len-1];
    while(--len){
        tmp = path[tmp];
        printf("%d\n",c[tmp].id);
    }
    return 0;
}
/*******
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
*****/

版权声明:本文博客原创文章,博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117192.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月9,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档