zoukankan      html  css  js  c++  java
  • HDOJ1160 FatMouse's Speed[dp](最长上升子序列)

    Problem Description
    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
     

    Input
    Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

    The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

    Two mice may have the same weight, the same speed, or even the same weight and speed.
     

    Output
    Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that

    W[m[1]] < W[m[2]] < ... < W[m[n]]

    and

    S[m[1]] > S[m[2]] > ... > S[m[n]]

    In order for the answer to be correct, n should be as large as possible.
    All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
     

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

    Sample Output
    4 4 5 9 7


    给你一堆老鼠的体重和跑的速度,要你找出体重大且速度小的一个序列,个数要最多。要严格大于和小于

    先按体重优先由小到大排序,然后求一遍速度的最长下降子序列就好了。

    也可以倒序排好然后求LIS。

    要输出这个序列,只能用n^2的算法。


    #include<iostream>
    #include<cassert>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<iterator>
    #include<cstdlib>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    #define debug(x) cout<<"debug "<<x<<endl;
    #define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i)
    #define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i)
    #define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i)
    #define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i)
    #define clr(c, x) memset(c, x, sizeof(c) )
    typedef long long int64;
    const int INF = 0x5f5f5f5f;
    const double eps = 1e-8;
    
    
    //*****************************************************
    typedef pair<int,int> P;
    #define F first
    #define S second
    struct Node
    {
        int id;
        P p;
        bool operator<(const Node & n2)const {return p < n2.p;}
    };
    Node a[1100];
    int d[1100];
    int prev[1100];
    void out(int i){
        if(i == 0)return;
        out(prev[i]);
        printf("%d
    ",a[i].id);
    }
    int main()
    {
        int n = 1;
        while(scanf("%d%d",&a[n].p.F, &a[n].p.S) == 2){
            a[n].id = n;
            ++n;
        }
        --n;
        sort(a+1,a+n+1);
        int ansn = 0,ansi=1;
        rep(i,1,n){
            d[i] = 1;
            dep(j,i-1,1){
                if(a[j].p.F == a[i].p.F)continue;
                if(a[i].p.S < a[j].p.S && d[i] < d[j]+1){
                    d[i] = d[j] + 1;
                    prev[i] = j;
                }
            }
            if(d[i] > ansn){
                ansi = i;
                ansn = d[i];
            }
        }
        printf("%d
    ",ansn);
        out(ansi);
    
        return 0;
    }
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    osg::PagedLOD example
    osg::NodeVisitor example
    osg::NodeVisitor
    osg::NodeVisitor osg3.4.0
    Visual studio 正在从以下位置加载符号:Microsoft符号服务器 尝试取消禁用后续符号加载
    osgViewer::Viewer::Windows
    Inventor2018专业版软件安装激活教程
    osg osgUtil::LineSegmentIntersector
    Civil 3D百度云地址
    osg define shape(create box)
  • 原文地址:https://www.cnblogs.com/DSChan/p/4861997.html
Copyright © 2011-2022 走看看