zoukankan      html  css  js  c++  java
  • HDU

    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;
    }
    

      

  • 相关阅读:
    教你如何剖析源码
    singleCall单来源调用解析及实现
    守护进程详细解读
    终端&作业控制&会话启动过程
    时间复杂度&空间复杂度
    linux环形buff模拟多线程信号量操作
    linux多线程-互斥&条件变量与同步
    linux线程控制&线程分离
    栈帧的不安全程序示例
    如何获取程序返回值,退出码,错误码
  • 原文地址:https://www.cnblogs.com/visualVK/p/9695758.html
Copyright © 2011-2022 走看看