10131 - Is Bigger Smarter?

UVA 1072解题报告
本文介绍了一种使用动态规划解决UVA 1072问题的方法,并通过回溯法输出最优解路径。该算法首先按特定条件对输入数据进行排序,然后利用动态规划找出最长递增子序列的长度及其构成元素。
//题目信息http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072
//本题主要是用动态规划。参考寂静山林的博客,写的非常详细(http://blog.youkuaiyun.com/metaphysis/article/details/6860405),这里只是做个个人随笔记录
//2012/2/28
//accepted
//
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;

#define MAXNUM 1000

void backtrack(int index);

class elephant
{
public:
int index;
int weight;
int iq;
bool operator < (const elephant &other) const
{
if(weight!=other.weight)
return weight<other.weight;
else return iq>other.iq;
}
};

elephant elephants[MAXNUM];
int length[MAXNUM];
int parent[MAXNUM];
int main()
{
int index=0,weight,iq;
int i,j;
while(cin>>elephants[index].weight>>elephants[index].iq)
{
elephants[index].index=index;
index++;
}

sort(elephants,elephants+index);
for(i=0;i<index;++i)
{
length[i]=1;
parent[i]=-1;
}

int maxLength=0,maxIndex=0;
for(i=0;i<index;++i)
{
int iq=elephants[i].iq;
int weigth=elephants[i].weight;

for(j=0;j<i;++j)
{
if(elephants[j].weight<weigth && elephants[j].iq>iq)
if(length[i] <= length[j])
{
length[i]=length[j]+1;
parent[i]=j;
}
}

if(maxLength<length[i])
{
maxLength=length[i];
maxIndex=i;
}
}
cout<<maxLength<<endl;
backtrack(maxIndex);
return 0;
}

void backtrack(int index)
{
if(parent[index]!=-1)
backtrack(parent[index]);
cout<<elephants[index].index+1<<endl;
}

动态规划,回溯法输出解,以及父节点记录的方法。。

Description Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to analyze a collection of elephants and place as large a subset of elephants as possible into a sequence whose weights are increasing but IQ's are decreasing. Input The input will consist of data for a bunch of elephants, at one elephant per line terminated by the end-of-file. The data for each 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 10,000. The data contains information on at most 1,000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ. Output The first output line should contain an integer n, the length of elephant sequence found. The remaining n lines should each contain a single positive integer representing an elephant. Denote the numbers on the ith data line as W[i] and S[i]. If these sequence of n elephants 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 must be as large as possible. All inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. Your program can report any correct answer for a given input. 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 根据以上要求编写代码使用c语言
06-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值