hdu 1160 FatMouse's Speed 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160
题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z)。每只老鼠有对应的weight 和 speed。现在需要从这 n 只老鼠的序列中,找出最长的一条序列,满足老鼠的weight严格递增,speed严格递减。
我们可以用一个结构体来保存老鼠的信息,包括weight, speed 以及 id(这个 id 是未排序之前的,为了输出最后信息)。那么首先对 weight 进行递增排序,如果遇到相同的weight,就对speed进行递减排序。那么固定了weight,我们就可以着眼于对speed的处理,此时问题就变成求最长递减序列啦。由于要保存路径信息,代码用path[]来保存,递归输出即可。
这条题目拖了好久啊,差不多一年了= =,太懒~~~
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ; struct node
{
int id;
int weight, speed;
}mouse[maxn];
int path[maxn];
int cnt[maxn]; int cmp(node a, node b)
{
if (a.weight == b.weight)
return a.speed > b.speed;
return a.weight < b.weight;
} void output(int pos)
{
if (!path[pos])
return;
output(path[pos]);
printf("%d\n", path[pos]);
} int main()
{
int w, sp, len = ;
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif while (scanf("%d%d", &w, &sp) != EOF)
{
mouse[len].weight = w;
mouse[len].speed = sp;
mouse[len].id = len;
cnt[len++] = -;
}
sort(mouse+, mouse+len, cmp);
for (int i = ; i < len; i++)
{
int k = ;
for (int j = ; j < i; j++)
{
if (mouse[j].speed > mouse[i].speed && mouse[j].weight < mouse[i].weight && k < cnt[j])
{
k = cnt[j];
path[mouse[i].id] = mouse[j].id;
}
}
cnt[i] = k;
cnt[i]++;
}
int ansid, ans = -;
for (int i = ; i < len; i++)
{
if (cnt[i] > ans)
{
ans = cnt[i];
ansid = mouse[i].id;
}
}
printf("%d\n", ans);
output(ansid);
printf("%d\n", ansid);
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(LIS+记录路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- 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 是按 ...
随机推荐
- 21.Android之SQLite数据库学习
Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLite 只需要带一个动 ...
- POJ 1789Truck History(pirme)
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22648 Accepted: 8781 De ...
- #pragma预处理实例
1.#include <stdio.h>#if defined(ANDROID20) #pragma message("Compile Android SDK 2.0... ...
- idea修改运行内存
安装目录下的bin 找到idea.exe.vmoptions 最大的修改下-Xmx1024m 找到idea64.exe.vmoptions 最大的修改下-Xmx1024m
- ecshop后台"云提醒未激活 点击激活" 补丁删除方法
ecshop后台"云提醒未激活 点击激活" 补丁删除方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2015-01-15 ecshop后台提 ...
- Python socket编程之六:多窗口的应用
import struct import sqlalchemy import pandas import matplotlib.pyplot as Plot from matplotlib.finan ...
- UIScrollview自动布局,UIScrollviewAutolayoutDemo
参考文档:http://www.cocoachina.com/ios/20150104/10810.html UIScrollviewAutolayoutDemo地址:http://pan.baidu ...
- pyenv 以及 virtualenv
244 pyenv global 3.5.1 245 which python 246 python 247 pip install virtualenv 248 ls 249 pwd 250 ls ...
- Starting zabbix_agentd: No such file or directory
问题描述 [root@localhost admin]# service zabbix_agentd restart Shutting down zabbix_agentd: [FAILED] Sta ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...