题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1160

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17386    Accepted Submission(s): 7694
Special Judge

Problem Description
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
 
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.

 
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that

W[m[1]] < W[m[2]] < ... < W[m[n]]

and

S[m[1]] > S[m[2]] > ... > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one. 

 
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
 
Sample Output
4
4
5
9
7
 
Source
 
 
题解:
1.根据体重,先对老鼠进行升序排序。设dp[i]为老鼠i的最大排位,亦是个数。 
2.对于每只老鼠i,枚举体重比它小, 速度比它快的老鼠j(由于经过了排序,所以j的范围为1~i-1),然后更新老鼠i的最大排位dp[i]。
3.dp[i]的最大值即为题目所求, 至于路径输出,设多一个pre[]数组,在跟新dp[i]的同时更新pre[i]就行了。
4.问:为什么要对老鼠进行排序,而不直接枚举呢?
  答:因为对于当前老鼠i,必须先求出体重比它小, 速度比它快的老鼠j的最终dp[j],如果不经过排序, 那么老鼠j可能放在了老鼠i的后面,dp[j]还没最终确定,就要求出dp[i],这样显然是行不通的,因为dp[i]的值是在dp[j]的最终值中转移过来的。换句话说, dp[i]和dp[j]的求解顺序是有明确要求的。
5.相同类型的题目:HDU1069
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; struct node
{
int wei, spd, id;
bool operator<(const node &x) {
return wei<x.wei;
}
}mice[MAXN]; int dp[MAXN], pre[MAXN]; void Print(int k) //输出路径
{
if(!k) return;
Print(pre[k]);
printf("%d\n", mice[k].id); //由于经过了排序,所以输出的是原始编号。
} int main()
{
int n = ;
int wei, spd;
while(scanf("%d%d", &wei, &spd)!=EOF)
{
mice[++n].wei = wei;
mice[n].spd = spd;
mice[n].id = n;
} sort(mice+, mice++n);
mice[].wei = -INF, mice[].spd = INF; //!!注意边界条件
memset(dp, , sizeof(dp));
memset(pre, , sizeof(pre)); int k = -;
for(int i = ; i<=n; i++)
for(int j = ; j<i; j++) //下标从0开始, 表明i作为第一个
{
if(mice[i].wei>mice[j].wei && mice[i].spd<mice[j].spd && dp[i]<dp[j]+)
{
dp[i] = dp[j]+;
pre[i] = j; //同时更新pre, 用于输出路径
}
if(k==- || dp[i]>dp[k])
k = i;
} printf("%d\n", dp[k]);
Print(k);
}

HDU1160 FatMouse's Speed —— DP的更多相关文章

  1. HDU 1160 FatMouse's Speed (DP)

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

  2. J - FatMouse's Speed dp

    题目链接: https://vjudge.net/contest/68966#problem/J 找最长子串并且记录路径. TLE代码: #include<iostream> #inclu ...

  3. HDU1160:FatMouse's Speed(最长上升子序列,不错的题)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1160 学的东西还是不深入啊,明明会最长上升子序列,可是还是没有A出这题,反而做的一点思路没有,题意就不多说 ...

  4. [HDU1160]FatMouse's Speed

    题目大意:读入一些数(每行读入$w[i],s[i]$为一组数),要求找到一个最长的序列,使得符合$w[m[1]] < w[m[2]] < ... < w[m[n]]$且$s[m[1] ...

  5. FatMouse's Speed

    J - FatMouse's Speed DP的题写得多了慢慢也有了思路,虽然也还只是很简单的DP. 因为需要输出所有选择的老鼠,所以刚开始的时候想利用状态压缩来储存所选择的老鼠,后面才发现n太大1& ...

  6. FatMouse's Speed 基础DP

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

  7. zoj 1108 FatMouse's Speed 基础dp

    FatMouse's Speed Time Limit: 2 Seconds      Memory Limit:65536 KB     Special Judge FatMouse believe ...

  8. zoj 1108 FatMouse's Speed 基础dp

    FatMouse's Speed Time Limit: 2 Seconds      Memory Limit:65536 KB     Special Judge FatMouse believe ...

  9. HDU 1160 FatMouse&#39;s Speed(DP)

    题意  输入n个老鼠的体重和速度   从里面找出最长的序列  是的重量递增时速度递减 简单的DP  令d[i]表示以第i个老鼠为所求序列最后一个时序列的长度  对与每一个老鼠i  遍历全部老鼠j  当 ...

随机推荐

  1. Github 多账号配置

    1. 不同账户,生成不同密钥ssh-keygen -t rsa -f github1 -C "xxx@163.com"ssh-keygen -t rsa -f github2 -C ...

  2. 关于 HTTP meta 的 IE=edge 说明

    http://www.oschina.net/question/54100_17414 陌生标记标记一: < meta http-equiv = "X-UA-Compatible&qu ...

  3. Centos6.5安装Oracle11.2.0.4 RAC(完整版)

    环境参数:Linux:Centos6.5 Grid和Oracle:11.2.0.4 一.环境配置 1.配置Node1和Node2两个节点之间的网卡 Node1: [root@rac1 network- ...

  4. BZOJ1573: [Usaco2009 Open]牛绣花cowemb

    求半径d<=50000的圆(不含边界)内n<=50000条直线有多少交点,给直线的解析式. 一开始就想,如果能求出直线交点与原点距离<d的条件,那么从中不重复地筛选即可.然而两个kx ...

  5. BZOJ 1123 tarjan

    题目链接 题意:一张无向图,把第$i$个点关联的所有边去掉,求无向图中有多少个点对不连通. 题解: 如果割的不是割点,那么总答案是$2\times (n-1)$. 如果是割点,要分别考虑每个子树的贡献 ...

  6. Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun

    因为是x,y均为整数因此对于同一直线的点,其最简分数x/y是相同的(y可以为0,这里不做除法)于是将这些点不断求最简分数用pair在set中去重即可. #include <cmath> # ...

  7. [bzoj2780][Spoj8093]Sevenk Love Oimaster_广义后缀自动机

    Sevenk Love Oimaster bzoj-2780 Spoj-8093 题目大意:给定$n$个大串和$m$次询问,每次给出一个字符串$s$询问在多少个大串中出现过. 注释:$1\le n\l ...

  8. poj——1274 The Perfect Stall

    poj——1274   The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25709   A ...

  9. html5 拖拽元素

    利用html5实现元素的拖拽,让拖动元素在指定的容器中拖动. 注意点:1.被拖元素要设置拖动属性.draggable="true" 2.容器元素要设置阻止默认事件处理 实现效果图如 ...

  10. 基于GDAL的栅格图像空间插值预处理

    转自 基于GDAL的栅格图像空间插值预处理——C语言版 基于GDAL的栅格图像预处理 前言 栅格数据和矢量数据构成空间数据的主要来源,怎样以开源方式读取并处理这些空间数据?目前有多种开源支持包,这里只 ...