动态规划(DP),类似LIS,FatMouse's Speed
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108
解题报告:
1、首先按照weight从小到大排列,weight相同的按照speed从大到小排列;
2、Count[i]表示到第i个老鼠时,所求的最长“速度递减”子序列的长度;
3、path[i]=j是题目的关键,记录在Count[i]=Count[j]时,即最长“速度递减”子序列最后一个老鼠的前一只老鼠的位置
4、递归输出id
void output(int path[],pos)
{
if(pos==) return ;
output(path,path[pos]);
printf("%d\n",mice[pos].id);
}
Sources Code:
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
#define INF 0x3f3f3f3f using namespace std; int n;///n只老鼠
int Count[]= {}; ///count[i]表示构造到第i个老鼠时,序列的最大长度
int path[]= {}; ///path[i]表示构造到第i个老鼠时的前一个老鼠(前驱) struct mouse
{
int weight;
int speed;
int id;
} mice[]; int cmp(const void *a,const void *b)
{
struct mouse *p1=(mouse *)a;
struct mouse *p2=(mouse *)b;
if(p1->weight==p2->weight)
{
if(p1->speed>p2->speed)
return p2->speed-p1->speed;
else return p1->speed-p2->speed;
}
else return p1->weight-p2->weight;
} void output(int path[],int pos)
{
if(pos==) return ;
output(path,path[pos]);
printf("%d\n",mice[pos].id);
} int main()
{
n=;
int i=,j=;
while(scanf("%d%d",&mice[++i].weight,&mice[++j].speed)!=EOF)
{
n++;
mice[n].id=n;
}
qsort(mice+,n,sizeof(mice[]),cmp);
Count[]=;
for(int i=; i<=n; i++)
{
for(int j=; j<i; j++)
{
if(mice[i].weight>mice[j].weight&&mice[i].speed<mice[j].speed)
{
if(Count[i]<Count[j])
{
Count[i]=Count[j];
path[i]=j;
}
}
}
Count[i]++;
}
int _max=;
int pos;
for(int i=; i<=n; i++)
{
if(Count[i]>_max)
{
_max=Count[i];
pos=i;
}
}
printf("%d\n",_max);
output(path,pos);
}
动态规划(DP),类似LIS,FatMouse's Speed的更多相关文章
- HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- FatMouse's Speed(HDU LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- FatMouse's Speed 基础DP
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- HDU 1160:FatMouse's Speed(LIS+记录路径)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- HDU-1160-FatMouse's Speed(线性DP,LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1160 FatMouse's Speed —— DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS ...
随机推荐
- ajax请求php,在返回信息前面出现了奇怪的红点点
如果你返回的json数据带有小红点,那么前台ajax是不认的,并且老是走ajax的error方法,不走success方法,因为ajax的dataType:“json”,你指定了返回的是json格式,j ...
- Python学习 day03
一.基本数据类型 python中的基本数据类型有以下几种: int -- 整数 python3中默认整数都是int型,python2中int的范围为-231~232-1(32位系统中)/ ...
- 6.ConcurrentHashMap jdk1.7
6.1 hash算法 就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所 ...
- 【3dsMax安装失败,如何卸载、安装3dMax 2012?】
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- 解决 command not found: express
需要先执行 sudo npm install -g express-generator 再安装 sudo npm install -g express 建立项目骨架 express -e xxx
- Android中dip, dp, px,pt, sp之间的区别:
Android中dip.dp.sp.pt和px的区别 1.概述 过去,程序员通常以像素为单位设计计算机用户界面.例如:图片大小为80×32像素.这样处理的问题在于,如果在一个每英寸点数(dpi)更 ...
- 3d旋转卡牌
做成向中心缩放就行了,和旋转效果一样的
- HZAU 18——Array C——————【贪心】
18: Array C Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 586 Solved: 104[Submit][Status][Web Boar ...
- 创建Django项目时,settings的静态文件的配置
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static'), )
- indexOf.substr,substring,charAt的区别
var a = "asdfghjkl" alert(a.substr(1, 3)); // 从下标为1开始,往右数3个长度的数, 显示 sdf; alert(a.s ...