HDU 1160:FatMouse's Speed(LIS+记录路径)
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20803 Accepted Submission(s): 9227
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
题意
有若干老鼠,每个老鼠都有体重和奔跑的速度。找到一个最长的老鼠序列,使老鼠的体重递增,速度递减,并输出这些老鼠的编号
思路
将老鼠的体重按照递增顺序排序,然后查找符合题目要求的老鼠序列,同时记录下老鼠排序之后的老鼠的编号。然后找到最长序列中的最后一个老鼠(体重最小,速度最大),然后递归寻找就行了
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,-1,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int dp[maxn];
// vis[i]=j 表示序列中第i个老鼠的前一个老鼠是第j个老鼠
int vis[maxn];
struct wzy
{
int weight,speed;
int id;
}p[maxn];
bool cmp(wzy u,wzy v)
{
return u.weight>v.weight;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int k=0;
while(cin>>p[k].weight>>p[k].speed)
{
p[k].id=k+1;
dp[k]=1;
k++;
}
sort(p,p+k,cmp);
int ans=0;
int res;
ms(vis);
for(int i=0;i<k;i++)
{
for(int j=0;j<i;j++)
{
if(p[i].weight<p[j].weight&&p[i].speed>p[j].speed)
{
if(dp[i]<dp[j]+1)
{
dp[i]=dp[j]+1;
vis[i]=j;
}
}
}
if(dp[i]>ans)
{
ans=dp[i];
res=i;
}
}
cout<<ans<<endl;
// res=-1时表示该序列已经输出完全
while(res!=-1)
{
cout<<p[res].id<<endl;
// 更新老鼠编号
res=vis[res];
}
return 0;
}
HDU 1160:FatMouse's Speed(LIS+记录路径)的更多相关文章
- HDU 1160 FatMouse's Speed 动态规划 记录路径的最长上升子序列变形
题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了 ...
- HDU 1160 FatMouse's Speed LIS DP
http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的 ...
- 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 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化
HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...
- hdu 1160 FatMouse's Speed(最长不下降子序列+输出路径)
题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
- hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
随机推荐
- js中如何返回一个存放对象的数组?
我这边需要返回后台数据的形式是这样的 {[ { ", }, { ", }, { ", }, { ", }, { ", } ]} 页面是通过循环去获取每 ...
- 从概率图模型pgm到rbm
有向图模型:directed acyclic graph DAG 贝叶斯网络 对称的,无向图, UGM: undirected graphic model UGM, 更有名的名称是MRF,mar ...
- linux 添加环境变量(php为例)
find / -name php vim /etc/profile 文件最后添加 export PATH=$PATH:/usr/local/php/bin source /etc/profile p ...
- HDU 1005 Number Sequence(数论)
HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...
- IOS应用内支付IAP从零开始详解
前言 什么是IAP,即in-app-purchase 这几天一直在搞ios的应用内购,查了很多博客,发现几乎没有一篇博客可以完整的概括出所有的点,为了防止大伙多次查阅资料,所以写了这一篇博客,希望大家 ...
- leetcode 刷题 数组类 Two Sum
---恢复内容开始--- Two Sum Given an array of integers ,find two numbers such that they add up to a specifi ...
- day21 MRO和C3算法
核能来袭 --MRO和C3算法 1. python的多继承 2.python经典类的MRO 3.python新式类的MRO, C3算法 4.super 是什么鬼? 一.python的多继承 在前面的学 ...
- 《Python》IO模型
一.IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下: 同步:一件事情做完再做另一件事情 异步:同时做多件事情 阻塞:sleep.input.join.shutdown.get.acquire ...
- linux系统安装mysql详细配置
参考文章https://baijiahao.baidu.com/s?id=1584072431498789934&wfr=spider&for=pc https://www.5yun. ...
- 通过泛型获得继承类的类原型getGenericSuperclass
首先贴上代码 package com; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * ...