HDU 1160 FatMouse's Speed
半个下午,总算A过去了
毕竟水题
好歹是自己独立思考,debug,然后2A过的
我为人人的dp算法
题意:
为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点
本着“指针是程序员杀手”这一原则,我果断用pre来表示这只老鼠的直接前驱的序号
代码中我是按体重从大到小排序,然后找出一条最长的体重严格递减速度严格递增的“链”(其实找到的是链尾)。
然后输出的时候从后往前输出
对结构体排序
对于样例来说,循环完以后应该是这样的:
| order | 2 | 3 | 4 | 8 | 1 | 5 | 7 | 0 | 6 |
| weight | 500 | 1000 | 1100 | 2000 | 6000 | 6000 | 6000 | 6008 | 8000 |
| speed | 2000 | 4000 | 3000 | 1900 | 2100 | 2000 | 1200 | 1300 | 1400 |
| lenth | 1 | 1 | 2 | 3 | 3 | 3 | 4 | 4 | 4 |
| pre | -1 | -1 | 3 | 4 | 4 | 4 | 8 | 8 | 8 |
pre == -1代表没有前驱。
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; struct Mouse
{
int order;
int weight;
int speed;
int pre;
int lenth;
}mice[]; int a[]; bool cmp1(Mouse a, Mouse b)
{
if(a.weight != b.weight)
return (a.weight > b.weight);
return (a.speed < b.speed);
} bool cmp2(Mouse a, Mouse b)
{
return (a.order < b.order);
} int main(void)
{
#ifdef LOCAL
freopen("1160in.txt", "r", stdin);
#endif int cnt = ;
int w, s;
while(scanf("%d%d", &w, &s) == )
{
mice[cnt].weight = w;
mice[cnt].speed = s;
mice[cnt].order = cnt;
++cnt;
} sort(mice, mice+cnt, cmp1);
int i, j;
for(i = ; i < cnt; ++i)
{
mice[i].lenth = ;
mice[i].pre = -;
}
//我为人人
for(i = ; i < cnt; ++i)
{
for(j = ; j < i; ++j)
{
if(mice[j].weight > mice[i].weight
&& mice[j].speed < mice[i].speed
&& mice[j].lenth+ > mice[i].lenth)
{
mice[i].lenth = mice[j].lenth + ;
mice[i].pre = mice[j].order;
}
}
} sort(mice, mice+cnt, cmp2);
int maxlen = ;
for(i = ; i < cnt; ++i)
if(mice[i].lenth > mice[maxlen].lenth)
maxlen = i;
int l = mice[maxlen].lenth;
printf("%d\n%d\n", l, maxlen+); int p = maxlen;
for(i = ; i < l; ++i)
{
printf("%d\n", mice[p].pre+);
p = mice[p].pre;
}
return ;
}
代码君
HDU 1160 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 ...
- HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化
HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...
- HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1160 FatMouse's Speed 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weigh ...
- HDU 1160 FatMouse's Speed LIS DP
http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的 ...
- HDU 1160 FatMouse's Speed (sort + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体 ...
- hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- HDU - 1160 FatMouse's Speed 【DP】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意 给出一系列的 wi si 要找出一个最长的子序列 满足 wi 是按照升序排列的 si 是按 ...
- 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
随机推荐
- Python:异常处理
Python 是面向对象的语言,所以程序抛出的异常也是类. 一.常见的异常类 NameError:尝试访问一个没有申明的变量 ZeroDivisionError:除数为 0 SyntaxError:语 ...
- 理解 Memory barrier
理解 Memory barrier(内存屏障) 发布于 2014 年 04 月 21 日2014 年 05 月 15 日 作者 name5566 参考文献列表:http://en.wikipedia. ...
- 【剑指offer】和为S的连续整数序列
找到所有和为S的连续整数序列,序列长度>=2 我的思路:数学法,限定首元素范围,计算序列长度. 书上解法:用small和big两个游标记录序列的开始和结束位置,调整游标. 我的解法: /* 直 ...
- oc和swift的混编
参考:http://blog.sina.com.cn/s/blog_8d1bc23f0102v5tl.html swift中使用oc类的方法 1.创建一个oc.h文件 2.添加需要倒入的oc类的头文件 ...
- (1)opengl-nehe 4种框架
http://www.yakergong.net/nehe/ 这个网站还是opengl方面比较权威的,作者叫nehe 这东西估计是要先装个ndk,然后才能运行代码 先睡觉! 以下内容参考自http:/ ...
- linux系统下挂载windows共享目录
在工作中有时我们需要在linux上挂载windows共享目录.首先我们需要学会在linux上查看windows共享了那些目录.查看操作需要安装samba-client. [root@ ~]# yum ...
- lintcode:两个数组的交
题目 返回两个数组的交 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2]. 解题 排序后,两指针找相等元素,注意要去除相同的元素 public class ...
- Linux系统新手学习的11点建议
随着Linux应用的扩展许多朋友开始接触Linux,根据学习Windwos的经验往往有一些茫然的感觉:不知从何处开始学起.这里介绍学习Linux的一些建议. 一.从基础开始:常常有些朋友在Linux论 ...
- MDX语法
https://msdn.microsoft.com/zh-cn/library/ms145506.aspx
- 整合Struts2+SiteMesh+Spring+MyFaces(JSF)+Freemarker的时候启动服务器报错ClassNotFoundException: org.apache.struts2.sitemesh.FreeMarkerPageFilter
我一琢磨,难道freemarker与struts2的整合也需要添加一个struts2-freemarker-plugin的jar包? 后来找了半天,确认不需要这个. 然后我就上网搜,这个FreeMar ...