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

题目:UVA 10131
Is Bigger Smarter

题意:给出大象的身高和体重,求身高递增且体重递减的最长序列,都是严格的,并打印序列。

分析:就是先对身高按自增排序,然后求一个单调递减子序列,严格单调的,所以加一句判断,然后打印序列,用一个数组保存就好了

开始想的是先预处理掉重复的,提交wa了,这样不行,因为你不知道体重是最高的还是最低的,可能开始留高的好,后面低的比较好。所以.....

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 2000;
const int inf = 0x3f3f3f3f;
struct Node
{
    int w,h;
    int num,dp;
};
vector<Node> v;
vector<int> ans;
int cmp(Node a,Node b)
{
    if(a.w!=b.w)
        return a.w<b.w;
}
int father[N];
int main()
{
    //freopen("Input.txt","r",stdin);
    int cnt=1;
    Node tmp;
    while(~scanf("%d%d",&tmp.w,&tmp.h)) //输入
    {
        tmp.num=cnt++;
        tmp.dp=0;
        v.push_back(tmp);
    }
    sort(v.begin(),v.end(),cmp);
    memset(father,0,sizeof(father));
    v[0].dp=1;
    bool ok=false;
    int count=0,ss=1,ttp=1;
    for(int i=1; i<v.size(); i++)
    {
        int ff=0,cas=0;
        for(int j=i-1; j>=0; j--)
        {
            if(v[i].h<v[j].h && v[i].w!=v[j].w)
            {
                if(ff<v[j].dp)
                {
                    ff=v[j].dp;
                    cas=j;
                }
            }
        }
        v[i].dp=ff+1;
        father[i]=cas;
        if(count<v[i].dp)
        {
            count=v[i].dp;
            ss=i;
            if(ok)
            {
                ttp=i;
                ok=true;
            }
        }
    }
    printf("%d\n",count);
    for(int i=ss;i>=ttp;i=father[i])
    {
        ans.push_back(v[i].num);
    }
    for(int i=ans.size()-1;i>=0;i--)
        printf("%d\n",ans[i]);
    v.clear();
    v.clear();
    ans.clear();
    return 0;
}
时间: 2024-10-11 21:33:25

UVA 10131 Is Bigger Smarter? 【严格单调递增子序列】的相关文章

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? (LIS,打印路径)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意:给定若干大象的体重及智商值.求满足大象体重严格递增,智商严格递减的序列的最大个数. 并打印随意一组取得最大值的序列的大象编号 分析:这个是LIS的应用,仅仅只是推断条件有两个,能够先对大象的体重排序,可是要打印路径. 那就必须得回溯求路径.能够直接逆序循环求,当然递归也是一个好的选择 #include<

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

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

最长单调递增子序列 POJ 3903 Stock Exchange .

题目传送门 : -------->点这里点这里<---------- 题目大意: 给出一串正整数,个数为N个.1<=N<=100000.求最长单调递增子序列长度. 样例输入: 6 5 2 1 4 5 3 3 1 1 1 4 4 3 2 1 样例输出: 3 1 1 =================================================================== 最长单调递增子序列问题的DP朴素算法复杂度为 O(n^2); 然而题目中的 N 最大有

单调递增子序列(二)

单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度. 如:1 9 10 5 11 2 13的最长单调递增子序列是1 9 10 11 13,长度为5. 输入 有多组测试数据(<=7) 每组测试数据的第一行是一个整数n表示序列中共有n个整数,随后的下一行里有n个整数,表示数列中的所有元素.每个整形数中间用空格间隔开(0<n<=1