Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131
题意:给定若干大象的体重及智商值。求满足大象体重严格递增,智商严格递减的序列的最大个数。
并打印随意一组取得最大值的序列的大象编号
分析:这个是LIS的应用,仅仅只是推断条件有两个,能够先对大象的体重排序,可是要打印路径。
那就必须得回溯求路径。能够直接逆序循环求,当然递归也是一个好的选择
#include<cstdio>
#include<algorithm>
using namespace std;
struct stu
{
int size,iq,id;
}a[1005];
int dp[1005],path[1005],m;
int cmp(struct stu a,struct stu b)
{
if(a.size!=b.size)
return a.size<b.size;
return a.iq>b.iq;
}
void back_path1(int i)
{
if(path[i]!=i)
back_path1(path[i]);
printf("%d\n",a[i].id);
}
void back_path2(int i)
{
if(m--){
back_path2(path[i]);
printf("%d\n",a[i].id);
}
}
int main()
{
int i=1,j,n,k,b[1005];
while(scanf("%d%d",&a[i].size,&a[i].iq)!=EOF){
a[i].id=i;
i++;
}
n=i-1;
sort(a+1,a+n+1,cmp);
for(i=1;i<=n;i++){
dp[i]=1;
path[i]=i;
for(j=1;j<i;j++)
if(a[j].size<a[i].size&&a[j].iq>a[i].iq&&dp[j]+1>dp[i]){
dp[i]=dp[j]+1;
path[i]=j;
}
}
k=1;
for(i=2;i<=n;i++)
if(dp[i]>dp[k])
k=i;
m=dp[k];
printf("%d\n",m);
b[1]=a[k].id;
i=2;
for(j=k;j>=1;j--) //直接逆序循环求路径
if(a[j].size<a[k].size&&a[j].iq>a[k].iq&&dp[k]==dp[j]+1){
b[i++]=a[j].id;
dp[k]--;
}
for(j=i-1;j>=1;j--)
printf("%d\n",b[j]);
//back_path1(k); //能够用两种递归求路径
//back_path2(k);
return 0;
}
Uva 10131 Is Bigger Smarter? (LIS,打印路径)的更多相关文章
- uva 10131 Is Bigger Smarter?(DAG最长路)
题目连接:10131 - Is Bigger Smarter? 题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输 ...
- 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?(DP最长上升子序列)
Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- 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?
动态规划题.类似UVa103 Stacking Box,都是题目给一种判断嵌套的方法然后求最长序列.提前对数据排序可以节省一些时间开销. 我的解题代码如下: #include <iostream ...
- What Goes Up UVA - 481 LIS+打印路径 【模板】
打印严格上升子序列: #include<iostream> #include<cstdio> #include<algorithm> #include<cst ...
- UVa 103 - Stacking Boxes (LIS,打印路径)
链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn}, 对于两个n维图形,求满足当中一个的全部边长 依照随意顺序都一一相应小于还有一个的边长,这种最长序列的个数,而且打 ...
- UVA - 10131Is Bigger Smarter?(DAG上的DP)
题目:UVA - 10131Is Bigger Smarter? (DAG) 题目大意:给出一群大象的体重和IQ.要求挑选最多的大象,组成一个序列.严格的体重递增,IQ递减的序列.输出最多的大象数目和 ...
随机推荐
- Cocos2d-x串算出Size方法
项目需要,根据所输入的字符串,的需要计算串帐户Size. 包代码如下面.只需要传递一个字符串,您可以返回Size: Size ChartDemoScene::calculateFontSize(con ...
- Linux 挂载NTFS文件系统
步骤如下: 1.安装ntfs-3g包 [root@CS-1 pub]# yum install ntfs-3g 2.创建挂载目录 [root@CS-1 pub]# mkdir /data 3.挂载NT ...
- Linux目录文件详解FHS标准(2013.09.05)
Linux 目录配置的依据FHS(Filesystem Hierarchy Standard)标准,将目录分成为四种交互作用的形态: 四种形态的具体解释: 可分享的:可以分享给其他系统挂载使用的目录, ...
- 启动和关闭JBoss As 7.1.1脚本
启动和关闭JBoss As 7.1.1,脚本例如以下djboss.sh: #!/bin/sh #JBOSS_HOME JBOSS_HOME=/opt/jboss case "$1" ...
- Effective C++ -- 构造析构赋值运算
05.了解C++默默编写并调用哪些函数 编译产生的析构函数时non-virtual,除非这个类的基类析构函数为virtual 成员变量中有引用和const成员时,无法自己主动生成copy assign ...
- Java与C/C++有什么区别
JDK包含JRE, 1-08: Helloworld: 01-08:classpath配置: 运行其它目录下的class文件: classpath一般不加分号,只找classpath下的文件: 后面加 ...
- hdu1542(线段树——矩形面积并)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 分析:离散化+扫描线+线段树 #pragma comment(linker,"/STA ...
- Ajax动态载入xml文件内容
<%@page import="javax.swing.JOptionPane"%> <%@page import="com.ctl.util.*&qu ...
- 实战:sqlserver 2008 扩展事件-XML转换为标准的table格式
--假设已经存在Event Session删除 IF EXISTS (SELECT * FROM sys.server_event_sessions WHERE name='MonitorLongQu ...
- C#-循环滚动字幕,timer,从左至右,从右至左,暂停---ShinePans
Lable的Left属性是能够更改的,可是 Right属性不能够更改,所以我们能够利用 这个特点做自加 自减运算 using System; using System.Collections.Gene ...