uva 10131 - Is Bigger Smarter?

该程序旨在从一组大象的数据中找出一个子序列,使得子序列中的大象体重递增,但智商递减。输入包含多头大象的体重和智商信息,程序通过排序和动态规划找到满足条件的最大长度子序列并输出。输出包括子序列的长度和具体的大象编号。题目要求输出尽可能长的满足条件的序列,并且所有比较必须严格,即体重和智商必须分别严格递增和递减。

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

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.

Input

The input will consist of data for a bunch of elephants, one elephant per line, terminated by the endof-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.

Output

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

 

题意:找出一段不需按照原定顺序的子列使其满足条件

排序一次然后再最小上升(或下降)子序列

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n=1,m,w[1005],s[1005],dp[1005],lat[1005];
struct B{
	int w,s,id;
}a[1005];
bool cmp(B i,B j)
{
	if(i.s == j.s)
	  return i.w < j.w ;
	return i.s > j.s ;
}
void ready()
{
	ios::sync_with_stdio(false),cin.tie(0);
	while(cin>>a[n].w >>a[n].s)
	{
		a[n].id=n;
		n++;
	}
	n--;
	sort(a+1,a+n+1,cmp);
 
}
void work_for_w()
{
	dp[1]=1; 
	for(int i=2;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
		{
			if(a[i].w>a[j].w && dp[j]+1>dp[i])
			{
				dp[i]=dp[j]+1;
				lat[i]=j;
			}
		}
	}
	int k=0,ans=0;
	for(int i=1;i<=n;i++)
	  if(dp[i]>dp[k])
	    k=i;
	while(k)
	{
		dp[++ans]=a[k].id;
		k=lat[k];
	}
	cout<<ans<<endl;
	for(int i=ans;i>=1;i--)
	  cout<<dp[i]<<endl;
}
int main()
{ 
	ready();
	work_for_w();
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值