ZOJ 1108 & HDU 1160 - FatMouse's Speed
题目大意:给你n只老鼠的体重w和速度s,让你找出最长的子序列使得w[i] < w[j] 且 s[i] > s[j] (i < j)。求最长序列的长度并输出该序列。
LIS(Longest Increasing Subsequence)问题,先对老鼠进行排序,以w为第一关键字从小到大排序,再以s为第二关键字从大到小排序,然后就是LIS问题的处理了。LIS(i)表示以a[i]结尾的最长增长序列的长度。
#include <cstdio>
#include <algorithm>
using namespace std; struct Mouse
{
int id, w, s;
bool operator < (const Mouse &m) const
{
if (w != m.w) return w < m.w;
return s > m.s;
}
} mouse[];
int lis[], pre[]; void print_lis(int p)
{
if (pre[p] != -) print_lis(pre[p]);
printf("%d\n", mouse[p].id);
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n = , a, b;
while (scanf("%d%d", &a, &b) != EOF)
{
mouse[n].w = a;
mouse[n].s = b;
mouse[n].id = n + ;
n++;
}
sort(mouse, mouse+n);
lis[] = ;
int p = ;
for (int i = ; i < n; i++)
pre[i] = -;
for (int i = ; i < n; i++)
{
lis[i] = ;
for (int j = ; j < i; j++)
if (mouse[i].w > mouse[j].w && mouse[i].s < mouse[j].s)
{
if (lis[j]+ > lis[i])
{
lis[i] = lis[j] + ;
pre[i] = j;
}
}
if (lis[i] > lis[p]) p = i;
}
printf("%d\n", lis[p]);
print_lis(p);
return ;
}
ZOJ 1108 & 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 是按 ...
随机推荐
- android 在代码中使用 #ffffff 模式 设置背景色
differentsum.setBackgroundColor(Color.parseColor("#F3733F"));
- POJ2718 递归套递归
就是给你一个数,排列组合,然后问如何排列之间的差值最小. 我之前的想法是一个递归,然后两个for循环枚举L1和L2,结果TLE了,然后想了一下剪枝发现没办法剪,然后看了一下别人的代码,用了next_p ...
- 解读QML之二
QML文档 QML文档是用QML语法组成的字符串.一个文档定义了一个QML对象类型.文档以”.qml”最为后缀,可以保存在本地和网络上,可以使用代码生成.一 个在文档中定义的对象类型的实例,也可以使用 ...
- List和ArrayList之间转换的例子
package Test01; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public c ...
- 强制设置IE浏览器的版本模式
转载自:http://blog.csdn.net/huwenhu2007/article/details/17954119 1.<meta http-equiv="X-UA-Compa ...
- 利用html5压缩图片,产出base64图片
/* 将页面选择的图片等比压缩成指定大小(长边固定) file:图片文件 callBack:回调函数 maxLen:长边的长度*/function makePic(file,callBack,maxL ...
- 阿里dom操作题
请写一个 getParents 方法让它可以获取某一个 DOM 元素的所有父亲节点. function getParents(id){ var obj=document.getElementById( ...
- base库插件---拖动
/** * Created by Administrator on 2014/6/5 0005. Base-drag 基于Base库的拖拽插件 tags为你要拖拽的元素参数, 数组形式传入 */ $( ...
- Python异常处理体系
1.Python内建异常体系结构 The class hierarchy for built-in exceptions is: BaseException +-- SystemExit +-- ...
- extjs入门
http://blog.csdn.net/xiebaochun/article/details/36414437