zoukankan      html  css  js  c++  java
  • HDU-1160-FatMouse's Speed(DP, 最长递增子序列)

    链接:

    https://vjudge.net/problem/HDU-1160

    题意:

    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.

    思路:

    对s排序, 然后最长递增子序列,nlogn总有bug还是n2好用

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const LL MOD = 20090717;
    const int MAXN = 1e3+10;
    
    struct Node
    {
        int w, s;
        int id;
        bool operator < (const Node& that) const
        {
            return this->s > that.s;
        }
    }node[MAXN];
    int Dp[MAXN], Pre[MAXN];
    int n;
    
    void Print(int p)
    {
        if (p == -1)
            return;
        Print(Pre[p]);
        printf("%d
    ", node[p].id);
    }
    
    int main()
    {
        int l, r;
        while (~scanf("%d%d", &l, &r))
        {
            node[++n].w = l;
            node[n].s = r;
            node[n].id = n;
        }
        sort(node+1, node+1+n);
        memset(Pre, -1, sizeof(Pre));
        memset(Dp, 0, sizeof(Dp));
        for (int i = 1;i <= n;i++)
        {
            Dp[i] = 1;
            for (int j = 1;j < i;j++)
            {
                if (node[i].w > node[j].w && node[i].s < node[j].s)
                {
                    if (Dp[j]+1 > Dp[i])
                    {
                        Dp[i] = Dp[j]+1;
                        Pre[i] = j;
                    }
                }
            }
        }
        int maxval = 1, maxpos = 1;
        for (int i = 1;i <= n;i++)
        {
            if (maxval < Dp[i])
            {
                maxval = Dp[i];
                maxpos = i;
            }
        }
        printf("%d
    ", maxval);
        Print(maxpos);
    
        return 0;
    }
    
  • 相关阅读:
    几个关于集合的有趣证明
    SICP 作业2.5
    有关集合大小的比较
    2020 ICPC 小米邀请赛 部分题解
    Hello World!
    【图论】BellmanFord算法
    【图论】格子图
    【图论】BFS
    【图论】二分图最大匹配 | 二分图最大独立集 | 二分图最小点覆盖
    【图论】最大权不相交区间
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11666550.html
Copyright © 2011-2022 走看看