andre_joy

导航

hdu 1160

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1160

题意:给出反例,老鼠长的越胖,速度越慢的例子。

mark:最长递增子序列。不过本题要求记录这段子序列,不能用传统的nlgn的算法,只能n*n的算法了。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
    int pos;
    int w,s;
}mice;

typedef struct
{
    int pre;
    int sum;
}dpp;

mice m[1010];
dpp dp[1010];

int cmp(const void *a, const void *b)
{
    mice *p = (mice *)a, *q = (mice *)b;
    if(p->s != q->s) return p->s - q->s;
    return q->w - p->w;
}

int main()
{
    int a,b,max1,pr,max2;
    int n = 1;
    int i,j,f;
    while(~scanf("%d%d", &a, &b))
    {
        m[n].pos = n;
        m[n].w = a;
        m[n++].s = b;
    }
    qsort(m, n, sizeof(mice), cmp);
    max2 = 0;
    for(i = 1; i < n; i++)
    {
        max1 = 0;
        for(j = i-1; j > 0; j--)
        {
            if(m[j].s < m[i].s && m[j].w > m[i].w && max1 < dp[j].sum)
            {
                max1 = dp[j].sum;
                pr = j;
            }
        }
        if(!max1)
        {
            dp[i].sum = 1;
            dp[i].pre = 0;
        }
        else
        {
            dp[i].sum = max1+1;
            dp[i].pre = pr;
        }
        if(max2 < dp[i].sum)
        {
            max2 = dp[i].sum;
            f = i;
        }
    }
    printf("%d\n", max2);
    while(dp[f].pre)
    {
        printf("%d\n", m[f].pos);
        f = dp[f].pre;
    }
    printf("%d\n", m[f].pos);
    return 0;
}

posted on 2012-07-29 23:12  andre_joy  阅读(649)  评论(0编辑  收藏  举报