HDU-1160-FatMouse's Speed(线性DP,LIS)
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20784 Accepted Submission(s): 9220
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
5
9
最长上升子序列问题变型,体重上升时要求速度下降(要按照这个顺序排序)并且要记录原始序号。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
struct Mouse
{
int w;
int v;
int on;
int parent;
}m[1001];
int cmp(Mouse n1, Mouse n2)
{
if(n1.w!=n2.w)
return n1.w < n2.w;
return n1.v > n2.v;
}
bool cmp1( Mouse n1, Mouse n2 )
{
return n1.on < n2.on;
}
int main()
{
int i, j, n;
int dp[1001] = { 0, };
i = 0;
while( ~scanf("%d %d", &m[i].w, &m[i].v) )
{
m[i].on = i + 1;
i++;
//if( i == 9 ) break;
}
n = i;
sort(m, m+n, cmp);
for( i=0; i<n; i++ )
{
for( j=0; j<i; j++ )
{
if( m[j].w < m[i].w && m[j].v > m[i].v )
{
if( dp[j] + 1 > dp[i] )
{
dp[i] = dp[j] + 1;
m[i].parent = m[j].on;
}
}
}
if( dp[i] == 0 )
{
dp[i] = 1;
m[i].parent = -1;
}
}
int x = 0;
int index;
for( i=0; i<n; i++ )
{
if( x <= dp[i] )
{
x = dp[i];
index = m[i].on;
}
}
cout << x << endl;
stack <int> S;
sort( m, m+n, cmp1 );
while( x-- )
{
S.push( index );
index = m[index-1].parent;
}
while( !S.empty() )
{
cout << S.top() << endl;
S.pop();
}
return 0;
}
HDU-1160-FatMouse's Speed(线性DP,LIS)的更多相关文章
- HDU 1160 FatMouse's Speed (sort + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体 ...
- HDU - 1160 FatMouse's Speed 【DP】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意 给出一系列的 wi si 要找出一个最长的子序列 满足 wi 是按照升序排列的 si 是按 ...
- HDU 1160 FatMouse's Speed(DP)
点我看题目 题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置. 思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题 ...
- HDU 1160 FatMouse's Speed ——(DP)
又是那个lis变形的题目. 但是不好定义严格的比较符号,因此只能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 (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化
HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...
- 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(最长不下降子序列+输出路径)
题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to ...
- HDU 1160 FatMouse's Speed (最长上升子序列)
题目链接 题意:n个老鼠有各自的重量和速度,要求输出最长的重量依次严格递增,速度依次严格递减的序列,n最多1000,重量速度1-10000. 题解:按照重量递增排序,找出最长的速度下降子序列,记录序列 ...
随机推荐
- leetcode 63 不同的路径2
描述: 从左上角走到右下角,中间可能有若干阻碍: 题目给出一个矩阵,0表示可以走,1表示有障碍. 解决: 思路同第一题,只是如果上面或左边有障碍,自身不一定能走,注意些边界条件即可,复杂度仍是m*n. ...
- Linux之chmod使用
Linux文件分三种身份和四中权限. u:文件的拥有者 g:文件所属的群组 o:其他用户 对于每个身份,又有四种权限,分别为: r:读取文件的权限(read) w:写入文件的权限(write) x:执 ...
- ie高版本浏览器不支持velocity的问题解决
<head><meta http-equiv="X-UA-Compatible" content="IE=5"></head> ...
- XP+Android手机DIY家庭视频点播系统-历时3周全力打造吊丝的幸福生活
需求场景(纯熟虚构): 1. 哥电脑里有200G电影copy到手机上看没那么大空间,copy一部看一部删除一部,很是不方便也费时间. 2. 小林同学需求比较旺盛但是媳妇总有不方便的时候,家里有 ...
- Spring3.x错误----java.lang.ClassNotFoundException:org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
Spring3.x错误: 解决方法: 缺少aspectjweaver.jar包 下载地址: https://cn.jarfire.org/aspectjweaver.html
- ZOJ2256 Mincost 2017-04-16 19:36 44人阅读 评论(0) 收藏
Mincost Time Limit: 2 Seconds Memory Limit: 65536 KB The cost of taking a taxi in Hangzhou is n ...
- 将Socket应用程序从Unix向Windows移植中应注意的几点问题
套接字(socket)当今已成为最流行的网络通信应用程序接口.套接字最初是由加利福尼亚大学Berkeley分校为Unix操作系统开发的网络通信接口,后来它又被移植到DOS与Windows系统,特别是近 ...
- 2.自己的Github注册流程
一开始申请Github,说实话我真的不知道它是什么东西,而且有什么用途.然后我就用360百科搜索了一下有关它的介绍:. 而说明的是Git是一个分布式的版本控制系统.然后我进入官方网站进行账号注册,而注 ...
- Provider 模式
Provider 模式:为一个API进行定义和实现的分离. 常见场景:DBPrider切换,第3方集成API切换 以发邮件为例: Email Provider Config: public abstr ...
- shell中调用jenkins API批量运行历史任务
shell中调用jenkins API批量运行jenkins带参数的任务: #!/bin/sh #startdate=20150127 startdate=20150201 while [ " ...