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 ...
随机推荐
- HDU 4171 Paper Route
Problem Description As a poor, tuition-ridden student, you've decided to take up a part time job as ...
- 命令行编译java项目
命令行编译java项目 项目名: testproj 目录 src -> cn -> busix -> test bin lib 编译项目 cd testproj javac -d . ...
- 结合manage.py,在flask项目中使用websocket模块--- flask-socketio
前言: - 为什么我要使用 flask-socketio模块,而不是flask-sockets? - 因为flask-socketio与前端流行的websocket库socke ...
- (四)selenium打开和关闭浏览器
一.Selenium简介 Selenium3.0主要变更特性: ①移除seleniumRC ②FireFox和Safari推出了自己的driver(geckodriver 和 Safaridriver ...
- ssm(spring、springmvc、mybatis)框架整合
第一次接触这3大框架,打算一个一个慢慢学,参照网上资料搭建了一个ssm项目,作为新手吃亏在jar包的导入上,比如jdbc DataSource配置的时候由于导入的jar包不兼容或者缺包导致项目无法正常 ...
- Java Mail邮件发送的简单实现
1.什么是java mail JAVA MAIL是利用现有的邮件账户发送邮件的工具,通过JAVA Mail的操控,让程序自动的使用设置的邮箱发送邮件. 这一机制被广泛的用在注册激活和垃圾邮件的发送等方 ...
- solidity合约面向对象
1. 属性[状态变量]的访问权限 public internal[合约属性默认的权限] private 说明:属性默认访问全向为internal,internal和private类型的属性,外部是 ...
- Ruby(或cmd中)输入命令行编译sass
Ruby(或cmd中)输入命令行编译sass步骤如下: 举例: 1.在F盘中新建一个总文件夹,比如test文件夹,其中在该文件夹下面建立html.images.js.sass等文件夹. 2.在sass ...
- Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题
1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...
- QTableview 只显示横向线
#include <QApplication> #include <QTableWidget> #include <QPainter> #include <Q ...