(最长上升子序列 并记录过程)FatMouse's Speed -- hdu -- 1160
http://acm.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): 12338 Accepted Submission(s): 5405
Special Judge
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.
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.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
#define N 10005
#define oo 0x3f3f3f3f /** 感觉有点用导弹防御的思想
每次记录从 0 到 i 的最长个数
并用 pre[i] 记录 i 点是从哪个点过来的 **/ /// number 是记录它原来的位置 struct node
{
int w, s, number;
}a[N]; /// pre[] 是个很好的记录方式,可以记录它的是从哪个过来的(它的上一步) int dp[N], pre[N]; /// 先按 w 从小到大排, 再按 s 从大到小排
int cmp(node n1, node n2)
{
if(n1.w!=n2.w)
return n1.w < n2.w;
return n1.s > n2.s;
} int main()
{
int i=, j, n; memset(a, , sizeof(a));
memset(dp, , sizeof(dp)); while(scanf("%d%d", &a[i].w, &a[i].s)!=EOF)
{
a[i].number = i;
i++;
} n=i;
sort(a+, a+i+, cmp); /**
for(i=1; i<n; i++)
printf("%d %d %d\n", a[i].number, a[i].w, a[i].s);
**/ for(i=; i<n; i++)
{
pre[i] = i;
dp[i] = ;
for(j=; j<i; j++)
{
if(a[i].w!=a[j].w && a[i].s<a[j].s)
{
if(dp[i]<dp[j]+)
{
pre[i] = j;
dp[i] = dp[j] + ;
}
}
}
} int Max=-oo, index=; for(i=; i<n; i++)
{
if(dp[i]>Max)
{
Max = dp[i];
index = i;
}
} printf("%d\n", Max);
i=;
int b[N]={};
while(pre[index]!=index)
{
b[i++] = a[index].number;
index = pre[index];
} printf("%d\n", a[index].number);
for(j=i-; j>=; j--)
printf("%d\n", b[j]); return ;
}
(最长上升子序列 并记录过程)FatMouse's Speed -- hdu -- 1160的更多相关文章
- FatMouse's Speed HDU - 1160 最长上升序列, 线性DP
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> usi ...
- ZOJ 1108 FatMouse's Speed (HDU 1160) DP
传送门: ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=108 HDU :http://acm.hdu.edu.cn/s ...
- HDU - 1160 最长上升子序列以及记录路径
题意:第一列,给出老鼠的重量,第二列,给出老鼠的速度,要证明老鼠的重量越大,速度越小,给出最多老鼠的数量,并说明第几只. 思路:先将老鼠按照重量从大到小排序,然后速度是从小到大,求最长上升子序列,学习 ...
- CSU 1225 最长上升子序列并记录其个数
;j<i;j++){ if(h[i] > h[j]){ ) cnt[i]+=cnt[j]; ) len[i] = len[j] + , cnt[i] = cnt[j]; } //身高相同的 ...
- - > 动规讲解基础讲解七——最长单增子序列
(LIS Longest Increasing Subsequence)给定一个数列,从中删掉任意若干项剩余的序列叫做它的一个子序列,求它的最长的子序列,满足子序列中的元素是单调递增的. 例如给定序列 ...
- HDU 1160 FatMouse's Speed(DP)
点我看题目 题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置. 思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题 ...
- HDU 1160 FatMouse's Speed 动态规划 记录路径的最长上升子序列变形
题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了 ...
- HDU - 1503 最长公共子序列记录路径
题意:先给两个水果的名字然后得出一个最短的序列包含这两个词. 思路:我一开始的思路是先求出最长公共子序列,然后做一些处理将其他的部分输出来:两种水果的字符串和最长公共子序列的字符串这三个字符串做对比, ...
- HDU 1160 FatMouse's Speed (最长上升子序列)
题目链接 题意:n个老鼠有各自的重量和速度,要求输出最长的重量依次严格递增,速度依次严格递减的序列,n最多1000,重量速度1-10000. 题解:按照重量递增排序,找出最长的速度下降子序列,记录序列 ...
随机推荐
- 10.9h5日记
一.单位 1.px是基本的单位,像素 2.em也是一个单位,使用方式,用元素父级的字体大小乘以em前的数字,父级没有就向上一个父级找, 直到body为止,如果body没有,就用默认的字体大小16px ...
- Sonar配置与使用
一.前置条件: 安装工具如下: JDK MySql服务器 SonarQube SonarScanner 从官网下载安装包进行安装,具体安装步骤略 二.配置 MySql配置: 打开SQL,创建sonar ...
- HTML标签归纳
首先,按下h1,再按下Tab键就可以变成<h1></h1>,Ctrl+d可以复制当前行,Ctrl+Alt+Insert可以快速新建文件,Home可以跳到当前行最前面,End同理 ...
- vs2015未能计算子级
数据源 属性里边值 设置出现问题
- PAT 1016 部分A+B(15)(C++&JAVA&&Python)
1016 部分A+B(15 分) 正整数 A 的"DA(为 1 位整数)部分"定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 A=3862767,D ...
- Xstream将XML转换为javabean的问题
1.问题:Xstream is not security 解决方法:加上 2.问题:如果没有第二行代码,会出现xstream forbiddenclassexception 解决方法:加上第二行,其中 ...
- BZOJ1057或洛谷1169 [ZJOI2007]棋盘制作
BZOJ原题链接 洛谷原题链接 设\(L[i][j],R[i][j],H[i][j]\)表示点\((i,j)\)向左.右.上尽量拓展的左端点.右端点.上端点的坐标. \(L,R\)直接初始化好,\(H ...
- mysql 5.7.17 建立主从数据库
---恢复内容开始--- 1.确定主/从数据库地址; 2.将主数据库的database dump出来 windows平台用workbench; 其他可以参考: 这位同仁写的比较详细了.我就不谢了.打开 ...
- Luogu 2279 [HNOI2003]消防局的设立 - 贪心
Description 给定一棵树形图, 建若干个消防站, 消防站能够覆盖到距离不超过2的点, 求最少需要建几个消防站才能覆盖所有点 Solution 从深度最深的点开始, 在它的爷爷节点上建, 每建 ...
- Luogu 2822[NOIP2016] 组合数问题 - 数论
题解 乱搞就能过了. 首先我们考虑如何快速判断C(i, j ) | k 是否成立. 由于$k$非常小, 所以可以对$k$分解质因数, 接着预处理出前N个数的阶乘的因数中 $p_i$ 的个数, 然后就可 ...