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. 题解:按照重量递增排序,找出最长的速度下降子序列,记录序列 ...
随机推荐
- [SoapUI]怎样运用Schema通过*.xsd文件来验证response对应的xml文件
添加Groovy Script脚本对Test Step进行验证 脚本如下(已经运行通过): import javax.xml.XMLConstants import javax.xml.transfo ...
- Weblogic的中的文件上传
在weblogic中在jsp页面中this.getServletContext().getRealPath("/upload")这样的写法是要报错的在jsp页面总你甚至不能使用th ...
- 一个用户管理的ci框架的小demo--转载
一个ci框架的小demo 最近在学习ci框架,作为一个初学者,在啃完一遍官方文档并也跟着官方文档的例程(新闻发布系统)做了一遍,决定在将之前练习PHP与MySQL数据库的用户管理系统再用ci框架实现一 ...
- 布局分析002:入门级的CSS导航弹出菜单
这种弹出菜单非常有意思,也有记录的意义,甚至可以说,掌握了这种弹出菜单,基本上CSS掌握的差不多. 主要涉及下面三个重要知识: CSS的继承性质. relative absolute定位. 子选择符& ...
- SDWebImage从缓存中获取图片
if ([[SDImageCache sharedImageCache] imageFromKey:sort.imageUrl]) { [cell.photoImageView s ...
- Knowing how all your components work together: distributed tracing with Zipkin
转自: http://aredko.blogspot.com/2014/02/knowing-how-all-your-components-work.html In today's post we ...
- Snapshot--使用Snapshot来还原数据库
在数据库升级时,为防止升级失败造成的影响,我们通常需要: 1.准备回滚脚本,用于失败后回滚 2.在升级前备份数据库,用于失败后恢复 但回滚脚本需要花费很长时间准备,而备份恢复会导致数据库长时间不可用, ...
- androidstudio提示adb错误:cannot parse version string:kg01的解决方法
打开adb.exe的文件目录,同时按下shift和鼠标右键,打开cmd后运行一下这个命令adb kill-server
- sql--CONVERT、FOR XML PATH解决实际问题
需求:每个平台分类下的门店,每个门店的名称.图片路径.评分,每个门店下的四个产品的名称.图片路径.评分 思路: 一开始门店动态化好写,用Ajax就行了.但是每个门店下面的产品,每个去请求一次查询有点不 ...
- MD5签名
/// <summary> /// 校验签名 /// </summary> /// <param name="mode ...