动态规划(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 ...
随机推荐
- 「BZOJ1485」[HNOI2009] 有趣的数列 (卡特兰数列)
「BZOJ1485」[HNOI2009] 有趣的数列 Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai ...
- 转帖 JS的基础语法2
条件语句(if.switch). 循环语句(while.do…while. for … in).跳转语句(break,continue) 1.条件语句 Ø if语句 javascrip中的if语句 v ...
- nginx 代理服务指令详解
nginx 正向代理与反向代理说明图 超级形象说明. 正向代理指令: 1, resolver 这个用于DNS服务器的ip . DNS服务器的主要工作是进行域名解析,将域名映射为对应IP地址 resol ...
- jacob自己动生成word文档目录
任务目的 1自动生成word文档目录. 用例测试操作步骤 在一个word文档的第二页填写占位符: {目录}保存.调用程序读取目标文档,自动根据标题生成目录到{目录}位置. 效果 关键代码 insert ...
- XLua 网络加载(基础操作)
LoadGameMethod 网上资源加载更新:加载场景中另建协程用来加载; public void LoadGameMethod() { StartCoroutine(start()); ...
- Ace教你一步一步做Android新闻客户端(五) 优化Listview
今天写存货了 调试一些动画参数花了些时间 ,嘿嘿存货不多了就没法做教程了,今天来教大家优化listview,等下我把代码编辑下 这次代码有些多 所以我把条理给大家理清楚.思路就是把加载图片的权利交给O ...
- Steamworks and Unity – P2P多人游戏
之前我们讨论过“如何把Steamworks.Net和Unity整合起来”,这是一个很好的开始,现在我们研究深一点,谈一谈Steam中的多人游戏.这不是教程,但是可以指导你在你的游戏中如何使用Steam ...
- ElasticSearch:集群(Cluster),节点(Node),分片(Shard),Indices(索引),replicas(备份)之间关系
[Cluster]集群,一个ES集群由一个或多个节点(Node)组成,每个集群都有一个cluster name作为标识----------------------------------------- ...
- pycharm Python解释器的配置--可以指定批处理文件为解释器
这样就可以很方便的配置一些环境变量了,很方便很有创意的功能,再次Mark一下以防忘记
- 微信小程序转百度小程序修改
百度小程序对比微信小程序(最初版):[设备]项里没有内存监控.iBeacon.wifi.蓝牙.用户截屏.手机联系人.NFC[位置]项里没有打开地图选择位置[界面]项里没有绘图功能.没有节点信息获取功能 ...