题目链接: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. angularjs的页面拆分思想

    //app.js angular.module('MyModule', ['SubModule1', 'SubModule2']) .module('SubModule1', ['CommonModu ...

  2. BZOJ3246 [Ioi2013]Dreaming

    Description Serpent(水 蛇)生活的地方有N个水坑,编号为0,...,N - 1,有M条双向小路连接这些水坑.每两个水坑之间至多有一条路径(路径包含一条或多条小路)相互连接,有些水坑 ...

  3. sql prompt5安装好了 也破解完成了 然后到SQL里面 还是没有提示 是为什么?

    勾上这个,祝你成功!

  4. OracleOraDb10g_home1TNSListener无法启动

    1:“本地计算机上的OracleOraDb10g_home1TNSListener服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 解决办法:动态ip,服务 OracleOraDb10 ...

  5. android studio问题-ICCP:Not recognizing known sRGB profile

    转:http://my.oschina.net/1pei/blog/479162 PNG格式:每个PNG文件是由一个PNG标识(signature),后面跟一些数据块(chunk),每个chunk由 ...

  6. easyUI框架之学习记录汇总

    在添加完之后,可以使用 $.parser.parse();这个方法进行处理:(1) 对整个页面重新渲染: $.parser.parse(); (2) 渲染某个特定的objectvar targetOb ...

  7. 浅谈PHP与手机APP开发(API接口开发)

    了解PHP与API开发 一.先简单回答两个问题: 1.PHP 可以开发客户端? 答:不可以,因为PHP是脚本语言,是负责完成 B/S架构 或 C/S架构 的S部分,即:服务端的开发.(别去纠结 GTK ...

  8. TEXT宏,TCHAR类型

    TCHAR *ptch = TEXT("This is a const string."); 如果使用UNICODE字符集, 则TEXT("This is a const ...

  9. Oracle存储过程中传入参数,传出字符串

    create or replace procedure getStr(usercode in varchar2,str out varchar2) is v_sql ); isEdit ); isAd ...

  10. 得分(Score,ACM/ICPC Seoul 2005,UVa 1585)

    #include<stdio.h> int main(void) { char b; int t,cou,sum; scanf("%d",&t); getcha ...