UVA 10131 Is Bigger Smarter?(DP最长上升子序列)
Description
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 nintegers 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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn=;
int d[maxn],pre[maxn],n; struct node
{
int x,y,id;
}p[maxn]; bool mycomp(node a,node b)
{
if(a.x!=b.x) return a.x<b.x;
return a.y>b.y;
} void printf_ans(int n,int i)
{
if(n==) return ;
int u=pre[i];
printf_ans(n-,u);
printf("%d\n",p[i].id);
} bool judge(int j,int i)
{
if(p[j].x<p[i].x&&p[j].y>p[i].y)
return true;
return false;
} int main()
{
int i,j;n=;
while(~scanf("%d%d",&p[n].x,&p[n].y))
{
p[n].id=n;n++;
}
n--;
sort(p+,p+n+,mycomp);
memset(pre,-,sizeof(pre));
memset(d,,sizeof(d));
p[].x=-;p[].y=;
for(i=;i<=n;i++)
{
for(j=;j<i;j++)
{
if(judge(j,i) && d[i]<d[j]+)
{
d[i]=d[j]+;pre[i]=j;
}
}
}
int ansm,ansi;
for(i=;i<=n;i++)
{
if(ansm<d[i])
{
ansm=d[i];ansi=i;
}
}
printf("%d\n",ansm);
printf_ans(ansm,ansi);
return ;
}
UVA 10131 Is Bigger Smarter?(DP最长上升子序列)的更多相关文章
- uva 10131 Is Bigger Smarter?(DAG最长路)
题目连接:10131 - Is Bigger Smarter? 题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输 ...
- UVA 10131 Is Bigger Smarter?(DP)
Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to t ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- UVA 10131 - Is Bigger Smarter? (动态规划)
Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. ...
- Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...
- UVa 10131: Is Bigger Smarter?
动态规划题.类似UVa103 Stacking Box,都是题目给一种判断嵌套的方法然后求最长序列.提前对数据排序可以节省一些时间开销. 我的解题代码如下: #include <iostream ...
- hdu 1025 dp 最长上升子序列
//Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...
- DP——最长上升子序列(LIS)
DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...
- UVA 103 Stacking Boxes n维最长上升子序列
题目链接:UVA - 103 题意:现有k个箱子,每个箱子可以用n维向量表示.如果一个箱子的n维向量均比另一个箱子的n维向量大,那么它们可以套接在一起,每个箱子的n维向量可以互相交换值,如箱子(2,6 ...
随机推荐
- npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\James\package.json'
在运行如下命令时, 遇到了问题: npm install --registry=https://registry.npm.taobao.org npm run dev 错误提示: 解决办法: 生成一个 ...
- 面向对象OO第一单元三次作业总结
(一)第一单元的作业围绕着多项式的求导,从简单到复杂,主要的要求是 作业一:只有两种格式的因子:带符号整数(+02)和幂函数(x^+02). 作业二:在作业一的基础上添加了:sin(x)和cos(x) ...
- CF-1114 (2019/02/11)
CF-1114 A. Got Any Grapes? skip B. Yet Another Array Partitioning Task 将n个数分成连续的k组,使得每组的前m大的数字的总和最大. ...
- Centos7离线部署kubernetes 1.13集群记录
一.说明 本篇主要参考kubernetes中文社区的一篇部署文章(CentOS 使用二进制部署 Kubernetes 1.13集群),并做了更详细的记录以备用. 二.部署环境 1.kubernetes ...
- 《linux设备驱动开发详解》笔记——12linux设备驱动的软件架构思想
本章重点讲解思想.思想.思想. 12.1 linux驱动的软件架构 下述三种思想,在linux的spi.iic.usb等复杂驱动里广泛使用.后面几节分别对这些思想进行详细说明. 思想1:驱动与设备分离 ...
- w3resource_MySQL练习: Aggregate_functions
w3resource_MySQL练习题:Aggregate_functions 1. Write a query to list the number of jobs available in t ...
- TextView设置缩略显示
1.代码设置 textview.setSingleLine(); textview.setEllipsiz(TextUtils.TruncateAt.valueOf("END")) ...
- 修改const变量
看下面的一段代码 ; int * j=(int*)(&i); // 运行正确,j确为i的地址,但 int *j=&i; 编译错误 *j=; //确实改变了i的值 printf(&quo ...
- 通用的高度可扩展的Excel导入实现(附Demo)
Demo源码 背景 通过程序将excel导入到数据库中是一项非常常见的功能.通常的做法是:先将excel转成DataTable,然后将DataTable转换成List<T>,最终通过Lis ...
- selenium - Js处理滚动条操作
# 11.Js处理滚动条操作 driver.execute_script('arguments[0].scrollIntoView();',target) target 为find_element_b ...