题目链接: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 解题报告的更多相关文章

  1. HDU 1160 FatMouse's Speed(要记录路径的二维LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 1160 FatMouse's Speed (DP)

    FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  3. HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化

    HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...

  4. HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...

  6. HDU 1160 FatMouse's Speed LIS DP

    http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的 ...

  7. HDU 1160 FatMouse's Speed (sort + dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体 ...

  8. hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...

  9. HDU - 1160 FatMouse's Speed 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意 给出一系列的 wi si 要找出一个最长的子序列 满足 wi 是按照升序排列的 si 是按 ...

随机推荐

  1. Android中用URL模拟一个简单的图片加载器

    首先,需要添加权限. <uses-permission android:name="android.permission.INTERNET"/> 整体代码如下: pac ...

  2. 【poj2724】 Purifying Machine

    http://poj.org/problem?id=2724 (题目链接) 题意 Mike有一个机器可以帮助他清理奶酪,每个奶酪由一个n位二进制数表示,机器上一共有n个按钮,每个按钮有1,0,*,其中 ...

  3. 洛谷 P1238 走迷宫

    因为小处疏漏,多花了半小时的水题 题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束 ...

  4. java判断list为空

    isEmpty()判断有没有元素而size()返回有几个元素 list.isEmpty()和list.size()==0 没有区别 list!=null跟!list.isEmpty()有什么区别? 这 ...

  5. Dinic 算法

    #include <bits/stdc++.h> using namespace std; ), M(1e5+); int head[N]; struct Edge{ /* r: resi ...

  6. Linux下添加新硬盘,分区及挂载

    挂载好新硬盘后输入fdisk -l命令看当前磁盘信息 可以看到除了当前的第一块硬盘外还有一块sdb的第二块硬盘,然后用fdisk /dev/sdb 进行分区 进入fdisk命令,输入h可以看到该命令的 ...

  7. linux 优化&安全运维&黑客攻防

    优化: 可删除用户:adm,lp,sync,shutdown,halt,news,uucp,operator,games,gopher.   :userdel games 可删除组:adm,lp,ne ...

  8. MySQL 高效分页

    create PROCEDURE USP_GetByPager( _pageindex int, _pagesize int ) BEGIN )*_pagesize; select * from A ...

  9. 无法卸载windows组件?提示zClientm.exe

    在卸载windows的组件: wmp, outlook, msessager等,提示: 找不到zClientm.exe 文件? 原来, zclientm.exe是windows的游戏组件依赖的文件, ...

  10. Word Amalgamation(枚举 + 排序)

    Word Amalgamation Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 373  Solved: 247 Description In mil ...