HDU 1160(两个值的LIS,需dfs输出路径)
传送门:
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): 20100 Accepted Submission(s): 8909
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.
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
4
5
9
7
#include<bits/stdc++.h>
using namespace std;
#define max_v 10050
struct node
{
int w,s,index;
}m[max_v];
int pre[max_v];
int dp[max_v];
bool cmp(node a,node b)
{
if(a.w!=b.w)
return a.w<b.w;
else
return a.s<b.s;
}
void dfs(int i)
{
int num=m[i].index;
if(i!=pre[i])
{
dfs(pre[i]);
}
printf("%d\n",num);
}
int main()
{
//w先升序sort一下,然后按照s做最长下降子序列,最后dfs输出该序列
int n=;
while(~scanf("%d %d",&m[n].w,&m[n].s))
{
m[n].index=n;
n++;
}
sort(m+,m++n,cmp);
pre[]=;
dp[]=;
for(int i=;i<=n;i++)
{
int maxx=;
int maxi=i;
for(int j=i-;j>=;j--)
{
if(m[i].s<m[j].s)
{
if(dp[j]>maxx)
{
maxx=dp[j];
maxi=j;
}
}
}
dp[i]=maxx+;
pre[i]=maxi;
}
int maxx=;
int maxi;
for(int i=;i<=n;i++)
{
if(maxx<dp[i])
{
maxx=dp[i];
maxi=i;
}
}
printf("%d\n",maxx);
dfs(maxi);
return ;
}
HDU 1160(两个值的LIS,需dfs输出路径)的更多相关文章
- HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化
HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...
- 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- hdu 5092 线裁剪(纵向连线最小和+输出路径)
http://acm.hdu.edu.cn/showproblem.php?pid=5092 给一个m*n的矩阵,找到一个纵向的"线"使得线上的和最小并输出这条线,线能向8个方向延 ...
- 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 排序或者通过最短路两种方法解决
题目大意: 给定一堆点,具有x,y两个值 找到一组最多的序列,保证点由前到后,x严格上升,y严格下降,并把最大的数目和这一组根据点的编号输出来 这里用两种方法来求解: 1. 我们可以一开始就将数组根据 ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 为什么HashMap初始大小为16,为什么加载因子大小为0.75,这两个值的选取有什么特点?
先看HashMap的定义: public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V> ...
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- unity消息队列
解决一些当一些消息事件需要处理时,但是 相应的系统还没有初始化来解决的问题 每个系统执行层也有一个消息队列,这样系统没有做好初始化,不执行就好了. 参考:http://blog.csdn.net/ws ...
- 1.2 js基础
1.onchange 99%用到select上边. 2.js是干什么的,修改css样式和属性 3.选项卡步骤 1.获取元素 2.循环给按钮加自定义属性 3.循环给按钮加事件 4.封装 ...
- 从函数作用域和块级作用域看javascript的作用域链
在ES6之前,javascript只有全局作用域和函数作用域.所谓作用域就是一个变量定义并能够被访问到的范围.也就是说如果一个变量定义在全局(window)上,那么在任何地方都能访问到这个变量,如果这 ...
- Unity3D第一课之自转与公转
1.物体公转,即围绕一个中心物体旋转 public class gongzhuan : MonoBehaviour { public GameObject Axis;//轴,用于选择围绕中心 publ ...
- Windows无法启动MySQL服务,错误1067
问题:mysql服务启动异常 找出原因 检查D:\mysql-5.6.20-winx64\data目录下client-02.err文件的错误信息(以err为后缀名的文件是mysql的日志文件) 修改配 ...
- PAT 1062 Talent and Virtue
#include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #i ...
- Javascript: hash tables in javascript
/** * Copyright 2010 Tim Down. * * Licensed under the Apache License, Version 2.0 (the "License ...
- 使用 iframe + postMessage 实现跨域通信
在实际项目开发中可能会碰到在 a.com 页面中嵌套 b.com 页面,这时第一反应是使用 iframe,但是产品又提出在 a.com 中操作,b.com 中进行显示,或者相反. 1.postMess ...
- Git 几个重要操作指令对比
1.git merge 和 git rebase https://blog.csdn.net/wh_19910525/article/details/7554489 http://gitbook.li ...
- 向jsp页面传值时出现乱码
在一个html页面中用表单向jsp页面传值: 这是html页面 <html> <head> <title>MyBeans.html</title> &l ...