zoukankan      html  css  js  c++  java
  • HDU 1160 FatMouse's Speed

    简单DP,最长上升子序列。先对W排序,然后对S做做LIS

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    const int maxn=10000+10;
    struct X
    {
        int w,s;
        int id;
    } x[maxn];
    
    bool cmp(const X&a,const X&b)
    {
        return a.w<b.w;
    }
    
    int pre[maxn],dp[maxn];
    stack<int>S;
    
    int main()
    {
        int n=0;
        while(~scanf("%d %d",&x[n].w,&x[n].s))
        {
            x[n++].id=n+1;
        }
        while(!S.empty())S.pop();
    
        sort(x,x+n,cmp);
    
        memset(dp,1,sizeof dp);
        memset(pre,-1,sizeof pre);
    
        for(int i=0; i<n; i++)
        {
            int Max=0,Pos=-1;
            for(int j=0; j<i; j++)
            {
                if(x[j].w<x[i].w)
                {
                    if(x[j].s>x[i].s)
                    {
                        if(dp[j]>Max)
                        {
                            Max=dp[j];
                            Pos=j;
                        }
                    }
                }
            }
            dp[i]=Max+1;
            pre[i]=Pos;
        }
    
        int ans=0,ansPos=-1;
        for(int i=0; i<n; i++)
        {
            if(dp[i]>ans)
            {
                ans=dp[i];
                ansPos=i;
            }
        }
    
        int nowPos=ansPos;
        while(1)
        {
            S.push(nowPos);
            nowPos=pre[nowPos];
            if(nowPos==-1) break;
        }
    
        printf("%d
    ",ans);
        while(!S.empty())
        {
            printf("%d
    ",x[S.top()].id);
            S.pop();
        }
    
        return 0;
    }
  • 相关阅读:
    办公自动化15-一次性生成多层目录
    小技巧1-查看excel中工作表(sheet)的个数
    LaTex 公式编辑
    Cpp 学习网站
    函数的凹凸性
    二项分布
    函数间断点
    霍夫丁(Hoeffding)不等式
    数域
    马尔可夫(Markov)不等式
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5138952.html
Copyright © 2011-2022 走看看