【解题报告】uva10131_Is Bigger Smarter?(越大越聪明, dp, LIS)

16 篇文章 0 订阅
12 篇文章 0 订阅

10131 - Is Bigger Smarter?

Time limit: 3.000 seconds

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 into a sequence so that the weights are increasing, but the IQ's are decreasing.

The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-file. The data for a particular elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. Both integers are between 1 and 10000. The data will contain information for at most 1000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ.

Say that the numbers on the i-th data line are W[i] and S[i]. Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing an elephant). If these n integers are a[1]a[2],..., a[n] then it must be the case that

   W[a[1]] < W[a[2]] < ... < W[a[n]]
and
   S[a[1]] > S[a[2]] > ... > S[a[n]]
In order for the answer to be correct,  n  should be as large as possible. All inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

Sample Input

6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample Output

4
4
5
9
7



题目大意:

输入多行数据,每行数据包括两个正整数W[i]和S[i],范围为[1,10000],分别代表一个大象的体重和IQ,输入至文件末尾,最多不超过1000个大象。从中选取n个大象构成一个序列,使得W[i]递增且S[i]递减(不可相等)。求满足条件的最长大象序列长度为多少,并输出任意一组大象编号的序列(编号按输入顺序)。


解题思路:

首先将所有大象按体重W[i]升序排列,这样该问题就转化为了最长上升子序列LIS(Longest Increasing Subsequence)问题。

定义状态d(i),代表前i个大象中以大象i作为序列结尾的最长上升子序列的长度。

状态转移方程:d(i) = max{ d(k)+1 | 1<=k<i, W[k]<W[i], S[k]>S[i] }

计算出所有状态后,再逆序枚举状态数组d[],加条件判断,打印出序列。


#include <cstdio>
#include <algorithm>
#define max(a,b) (a>b?a:b)

using std::sort;

const int N=1010;
int d[N];//状态
int n=1;

struct _ele{
    int i;//编号
    int w;//weight
    int s;//IQ
    bool operator<(const _ele A)const{
        return w<A.w;
    }
}ele[N];

int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&ele[n].w,&ele[n].s)) ele[n].i=n,++n;
    sort(ele+1,ele+n);
    //for(int i=1;i<n;++i) printf("%d: %d %d\n",i,ele[i].w,ele[i].s);

    for(int i=1;i<n;++i) d[i]=1;//init

    for(int j=2;j<n;++j){//以j结尾
        for(int i=1;i<j;++i){//枚举前j-1个状态
            if(ele[i].w<ele[j].w && ele[i].s>ele[j].s){
                d[j]=max(d[i]+1, d[j]);
            }
        }
    }
    //for(int i=1;i<n;++i) printf("%d ",d[i]);printf("\n");

    int max_v=-1,max_i;
    for(int i=1;i<n;++i) if(max_v<d[i]){
        max_v=d[i],max_i=i;
    }
    printf("%d\n",max_v);

    int res[N],i_r=0;
    int pre_s=-1,pre_w=10010,pre_d=max_v+1;
    for(int i=max_i;i>0;--i){
        if(ele[i].s>pre_s && ele[i].w<pre_w && d[i]==pre_d-1){
            pre_s=ele[i].s;
            pre_w=ele[i].w;
            pre_d-=1;
            res[i_r++]=i;
        }
    }
    //for(int i=i_r-1;i>=0;--i) printf("%d\n",res[i]);
    for(int i=i_r-1;i>=0;--i) printf("%d\n",ele[res[i]].i);

    return 0;
}



"Got a packet bigger than 'max_allowed_packet' bytes" 这个错误是MySQL数据库的一个错误提示。它表示一个数据包的大小超过了配置的最大允许值。这个错误可能会在向数据库中插入或更新大型数据时发生。 为了解决这个问题,可以调整 MySQL 的 max_allowed_packet 参数的值。这个参数定义了单个数据包的最大大小。可以通过修改配置文件、在命令行中指定参数或者在 MySQL 实例中执行 SET GLOBAL max_allowed_packet 命令来修改该参数的值。 例如,可以通过在命令行中执行以下命令来设置 max_allowed_packet 的值为 32MB: ``` mysql --max_allowed_packet=32M ``` 这样设置之后,MySQL 就可以处理更大的数据包了,从而避免了 "Got a packet bigger than 'max_allowed_packet' bytes" 错误的发生。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [[Err] 1153 - Got a packet bigger than 'max_allowed_packet' bytes(linux环境).pdf](https://download.csdn.net/download/lvlei19911108/20838930)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Got a packet bigger than ‘max_allowed_packet’ bytes?问题解决](https://blog.csdn.net/HCW_wei/article/details/121397922)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Got a packet bigger than 'max_allowed_packet' bytes 问题的解决方法](https://blog.csdn.net/qq_43293244/article/details/85114537)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值