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 ...
随机推荐
- C#与C++之间类型的对应{转}
Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR ...
- 深入了解linux下的last命令及其数据源
http://www.9usb.net/200902/linux-last.html http://blog.csdn.net/chaofanwei/article/details/11826567
- MY_Log,无缝替换原生Log,支持日志输出到文件、FirePHP
自己扩展了一个MY_Log, 用法类似于log4j,目前支持将日志输出到文件.FirePHP.如果你需要将日志输出到其他地方,比如邮件.数据库等,可以很方便地进行扩展. 用法很简单,大家一看就知道.1 ...
- (转)STL中set的用法
转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记 ...
- java for循环的几种写法
J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象.本文介绍使用这种循环的具体方式,说明如何自行定义能被这样遍历的类 ...
- JDK安装(windows/linux)
双击安装...安装之后需要进行一些相关的配置工作...下面是我自己总结的安装和配置步骤: (1)非Win7系统 第一步:安装jdk,下载地址:http://www.oracle.com/technet ...
- vim不保存退出
对于刚开始使用vi/vim文本编辑器的新手来说,如何在不保存更改而退出vi/vim 文本编辑器呢? 当你使用linux vi/vim 文本编辑器对linux下某个配置文件做编辑操作,当你更改完之后,可 ...
- Struts2笔记——与ServletAPI解耦
与ServletAPI解耦的访问方式 为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSessi ...
- DWR与AJAX
DWR与AJAX的微妙关系 2015-08-14 10:20 447人阅读 评论(0) 收藏 举报 本文章已收录于: // ' + obj.name + " "; html ...
- 等宽格子堆砌 js
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...