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. 题解:按照重量递增排序,找出最长的速度下降子序列,记录序列 ...
随机推荐
- Dockerfile RUN, CMD & ENTRYPOINT
Dockerfile RUN, CMD & ENTRYPOINT 在使用Dockerfile创建image时, 有几条指令比较容易混淆, RUN, CMD, ENTRYPOINT. RUN是在 ...
- 引入jQuery
用于测试和开发(未压缩,是可读的代码) uncompressed, 用于实际的网站中,已被精简和压缩. minified jQuery 1.xjQuery Migrate 过渡版jQuery ...
- xib下这种方式创建cell
这种方法在iOS5.0之前是不能够创建成功的. MEConvertListTableViewCell *cell = [tableView dequeueReusableCellWithIde ...
- Ubuntu的常识使用了解2
1. 在linux系统中,所有的目录(分区)都是挂靠在/跟目录下. 使用「cd」这个指令来切換目录.切换目录的方式:(1)绝对路径,以根目录做开头/, (2)相对目录, 没有以根目录开头. 注意:插补 ...
- UVALive - 6434 —(思维题)
题意:给出了你由n个数组成的序列,让你将这个序列分为成m个集合,使得每一个集合的最大值减最小值的差相加最小.(如果某集合只有一个数字,则最大值减最小值为0) . 思路:首先我们不难想到,最优的分配方法 ...
- FNDLOAD Commands to Download Different Seed Data Types. (DOC ID 274667.1)
In this Document Goal Solution References Applies to: Oracle Application Object Library - Version 11 ...
- Centos7 Minni 安装 执行ifconfig命令出现 -bash ifconfig command not found 的解决方法
1) have a root privilege shell or be on the sudo list. 2a) At a root shell prompt (#) yum install ne ...
- MSP430G2553 Launchpad 硬件I2C驱动
一.USCI I2C 驱动介绍 对于MSP430G2553,硬件I2C由外设USCI(Universal Serial Communication Interface)提供.USCI又分为USCI_A ...
- CentOS 7安装OpenVPN
命令很简单,但是网上似乎没资料,只有RHEL6以下的资料. 直接贴命令: 1 2 3 4 5 cd ~ wget http://swupdate.openvpn.org/as/openvpn-as-2 ...
- 查看JVM
通过jconsole.exe来查看,jconsole.exe所在目录为/%JAVA_HOME%/bin/jconsole.exe 需要配置: -Dcom.sun.management.jmxrem ...