Uva 10131 Is Bigger Smarter? (LIS,打印路径)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131

题意:给定若干大象的体重及智商值。求满足大象体重严格递增,智商严格递减的序列的最大个数。

并打印随意一组取得最大值的序列的大象编号

分析:这个是LIS的应用,仅仅只是推断条件有两个,能够先对大象的体重排序,可是要打印路径。

那就必须得回溯求路径。能够直接逆序循环求,当然递归也是一个好的选择

#include<cstdio>
#include<algorithm>
using namespace std;
struct stu
{
    int size,iq,id;
}a[1005];
int dp[1005],path[1005],m;
int cmp(struct stu a,struct stu b)
{
    if(a.size!=b.size)
        return a.size<b.size;
    return a.iq>b.iq;
}
void back_path1(int i)
{
    if(path[i]!=i)
        back_path1(path[i]);
    printf("%d\n",a[i].id);
}
void back_path2(int i)
{
    if(m--){
        back_path2(path[i]);
        printf("%d\n",a[i].id);
    }
}
int main()
{
    int i=1,j,n,k,b[1005];
    while(scanf("%d%d",&a[i].size,&a[i].iq)!=EOF){
        a[i].id=i;
        i++;
    }
    n=i-1;
    sort(a+1,a+n+1,cmp);
    for(i=1;i<=n;i++){
        dp[i]=1;
        path[i]=i;
        for(j=1;j<i;j++)
            if(a[j].size<a[i].size&&a[j].iq>a[i].iq&&dp[j]+1>dp[i]){
                dp[i]=dp[j]+1;
                path[i]=j;
            }
    }
    k=1;
    for(i=2;i<=n;i++)
        if(dp[i]>dp[k])
            k=i;
    m=dp[k];
    printf("%d\n",m);
    b[1]=a[k].id;
    i=2;
    for(j=k;j>=1;j--)    //直接逆序循环求路径
        if(a[j].size<a[k].size&&a[j].iq>a[k].iq&&dp[k]==dp[j]+1){
            b[i++]=a[j].id;
            dp[k]--;
        }
    for(j=i-1;j>=1;j--)
        printf("%d\n",b[j]);
    //back_path1(k);             //能够用两种递归求路径
    //back_path2(k);
    return 0;
}
时间: 2024-10-07 10:07:01

Uva 10131 Is Bigger Smarter? (LIS,打印路径)的相关文章

UVA 10131 Is Bigger Smarter? 【严格单调递增子序列】

题目:UVA 10131 Is Bigger Smarter 题意:给出大象的身高和体重,求身高递增且体重递减的最长序列,都是严格的,并打印序列. 分析:就是先对身高按自增排序,然后求一个单调递减子序列,严格单调的,所以加一句判断,然后打印序列,用一个数组保存就好了 开始想的是先预处理掉重复的,提交wa了,这样不行,因为你不知道体重是最高的还是最低的,可能开始留高的好,后面低的比较好.所以..... AC代码: #include<iostream> #include<cstdio>

uva 10131 Is Bigger Smarter? (DAG)

uva 10131 Is Bigger Smarter? 题目大意:当一只大象的体重大于另一只的体重,且智商小于另一只的智商,该大象便可以"嵌套"另一只大象.问,最长的嵌套方式.(答案不唯一) 解题思路:DAG. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; struct ELE{ int w

UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible in

UVa 10131 Is Bigger Smarter? (LDS+数据结构排序)

Is Bigger Smarter? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, y

uva 10131 Is Bigger Smarter? dag 最长路 加路径还原

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> #define INF 10000

UVA 10131 Is Bigger Smarter?(DP)

Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but t

UVa 10131 - Is Bigger Smarter?

题目:有人认为大象的体重和智力有一定的正相关性,现在给你一些数据,找到一个最长的反例序列. 分析:dp,LIS,醉倒上升子序列.对W排序求出S的最大下降子序列即可,存储路径前驱,dfs输出. 说明:先读到EOF再处理. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath

What Goes Up UVA - 481 LIS+打印路径 【模板】

打印严格上升子序列: #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<qu

UVa 103 - Stacking Boxes (LIS,打印路径)

链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足其中一个的所有边长 按照任意顺序都一一对应小于另一个的边长,这样的最长序列的个数,并且打印任意一个最长子串的路径, 例如:a(9,5,7,3),b(6,10,8,2),c(9,7,5,1),a和b不满足,但c和b满足 分析:首先对没组边长从小到大排序,再对各组图形按最小边排序,再求最大子串, 对于打印路径,可以逆序循环,也可递归求解 #include<cstdio> #include