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
题目分析:要求找出重量从小到大、智商从大到小排列的最长子序列。
解题思路:1、对大象按重量从小到大排序。
2、从前向后遍历所有大象,开辟动态滚动数组dp[i]代表当前大象为终点形成的序列长度,dp[j]代表以i之前符合要求的大象为终点的序列长度,如果dp[j]+1>dp[i],则dp[i]=dp[j]+1,用路径path[i]记下j(i之前那头大象)。
3、输出最长序列的长度,并递归输出路径。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct ele
{
int w,s,id;
}a[1005];
int dp[1005],path[1005],MAX=-1;
bool comp(ele a1,ele a2)
{
return a1.w<a2.w;
} /*递归输出路径*/
void printpath(int i)
{
if(MAX--)
{
printpath(path[i]);
printf("%d\n",a[i].id);
}
} int main()
{
int i,j,p,k;
k=1;
while(~scanf("%d%d",&a[k].w,&a[k].s))
{
a[k].id=k;
k++;
}
k--;
sort(a+1,a+k,comp);
for(i=1;i<=k;i++)
{
dp[i]=1;
path[i]=i;
}
for(i=1;i<=k;i++)
{
for(j=1;j<i;j++)
{
if(a[i].w>a[j].w&&a[i].s<a[j].s&&dp[i]<dp[j]+1)
{
dp[i]=dp[j]+1;
path[i]=j; /*记录i之前那头大象*/
}
if(dp[i]>MAX)
{
MAX=dp[i];
p=i;
}
}
}
printf("%d\n",MAX);
printpath(p);
return 0;
}
												

最新文章

  1. 阿里云 centos 安装apache和php
  2. zlhome.com Deal
  3. struts.xml 配置详解
  4. cocos2d对动画的各种操作
  5. C++ Primer : 第十二章 : 动态内存之shared_ptr类实例:StrBlob类
  6. ZRender源码分析2:Storage(Model层)
  7. quartzJob 例子
  8. python之配置日志的三种方式
  9. Hibernate-day02
  10. zabbix 自带监控项报性能问题解决方法
  11. hashcode相等两个类一定相等吗?equals呢?相反呢?
  12. javaweb之Cookie学习
  13. Flask学习【第10篇】:自定义Form组件
  14. linux二进制安装MariaDB
  15. 用java打暴雪星际争霸(2)——执行測试机器人
  16. 测试Js权限
  17. 上传指定url文件到阿里云oss
  18. 关于java.lang.IncompatibleClassChangeError: Implementing class错误解决
  19. php的explode()和split()的区别
  20. JS判断不能为空实例代码

热门文章

  1. POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)
  2. (转载)PHP strtotime函数详解
  3. 动态规划(斜率优化):[CEOI2004]锯木厂选址
  4. IIS7授权错误:“无法验证对路径的访问”的解决方法
  5. poj 3903 最长上升子序列 Stock Exchange
  6. cobbler客户端重装系统
  7. selenium grid java 资料
  8. 【每日一linux命令8】添加新的工作组(groupadd)
  9. jQuery常用事件详解
  10. 代码对齐 分类: C#小技巧 2014-04-17 14:45 166人阅读 评论(0) 收藏