[动态规划]UVA10131 - Is Bigger Smarter?

本文探讨了如何通过一组大象的数据来反证体型与智力之间的错误关联。通过对数据进行处理,利用动态规划算法找出最大子集,使得该子集中大象的体重递增而智商递减。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:

有些人认为大象是越大只的越聪明​​。为了要反证这项说法,你想要用收集到的大象资料列出一个大象数目最大的子集合,在这个子集合中,大象的体重是渐增的,而智商是渐减的。

Input

只有一组测试资料,包含了最多1000只大象的体重及智商的资料。每只大象一列,有2个整数W i及S i(介于1和10000之间)分别代表第i只大象的体重及智商。大象的编号从1开始。

不同的大象可能有相同的重量,相同的智商,或相同的重量及智商。

Output

第一列输出一个整数n,代表你可以找到的子集合最大的大象数目。接下来的n 列,每列有一个正整数,代表某只大象的编号。

如果这n个正整数是a1,a2,....an,那它应该要符合

a1 < W a2 < ...... < W an,且

a1 > S a2 > ...... > S an

解答可能不只一个,请输出其中任何一个即可。

思路:DAG上的动态规划,比较典型,先建立图的模型,使用动态规划就比较简单。

#include<iostream>
#include<cstring>

using namespace std;

class Node
{
public:
    int w,s;
}node[1050];

int map[1050][1050];
int d[1050],num;

int dp(int i)
    {
        int& ans=d[i];
        if(ans>0) return ans;
        ans=1;
        for(int j=1;j<=num;j++)
            {
                if(map[i][j]==1)
                    {
                        int cnt=dp(j);
                        if(cnt+1>ans) ans=cnt+1;
                    }
            }
        return ans;
    }

void print_dp(int pos)
    {
        cout<<pos<<endl;
        for(int i=1;i<=num;i++)
            {
                if(map[pos][i]==1&&d[pos]==d[i]+1)
                    {
                        print_dp(i);
                        break;
                    }
            }
    }

int main()
    {
        int x,y;
        num=0;
        while(cin>>x>>y)
            {
                if(x==0&&y==0) break;
                node[++num].w=x;
                node[num].s=y;
            }
        int i,j,k;
        memset(map,0,sizeof(map));
        memset(d,0,sizeof(d));
        for(i=1;i<=num;i++)
            {
                for(j=1;j<=num;j++)
                    {
                        if(node[i].w<node[j].w&&node[i].s>node[j].s) map[i][j]=1;
                    }
            }
        int cnt=0,pos;
        for(i=1;i<=num;i++)
            if(dp(i)>cnt) cnt=dp(i),pos=i;
        cout<<cnt<<endl;
        print_dp(pos);
        return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值