博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ1160 FatMouse's Speed[dp](最长上升子序列)
阅读量:5285 次
发布时间:2019-06-14

本文共 2835 字,大约阅读时间需要 9 分钟。

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

给你一堆老鼠的体重和跑的速度,要你找出体重大且速度小的一个序列,个数要最多。要严格大于和小于

先按体重优先由小到大排序,然后求一遍速度的最长下降子序列就好了。

也可以倒序排好然后求LIS。

要输出这个序列,只能用n^2的算法。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define debug(x) cout<<"debug "<
<
= _end_; --i)#define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i)#define clr(c, x) memset(c, x, sizeof(c) )typedef long long int64;const int INF = 0x5f5f5f5f;const double eps = 1e-8;//*****************************************************typedef pair
P;#define F first#define S secondstruct Node{ int id; P p; bool operator<(const Node & n2)const {return p < n2.p;}};Node a[1100];int d[1100];int prev[1100];void out(int i){ if(i == 0)return; out(prev[i]); printf("%d\n",a[i].id);}int main(){ int n = 1; while(scanf("%d%d",&a[n].p.F, &a[n].p.S) == 2){ a[n].id = n; ++n; } --n; sort(a+1,a+n+1); int ansn = 0,ansi=1; rep(i,1,n){ d[i] = 1; dep(j,i-1,1){ if(a[j].p.F == a[i].p.F)continue; if(a[i].p.S < a[j].p.S && d[i] < d[j]+1){ d[i] = d[j] + 1; prev[i] = j; } } if(d[i] > ansn){ ansi = i; ansn = d[i]; } } printf("%d\n",ansn); out(ansi); return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/DSChan/p/4861997.html

你可能感兴趣的文章
An easy problem
查看>>
MauiMETA工具的使用(一)
查看>>
LeetCode: Anagrams 解题报告
查看>>
Qt 中获取本机IP地址
查看>>
070102_赌博设计:概率的基本概念,古典概型
查看>>
IT人生的价值和意义 感觉真的有了
查看>>
JS DOM对象
查看>>
OGR – Merging Multiple SHP files
查看>>
创业公司该不该被收购?(转)
查看>>
sqlserver 行转列、列转行[转]
查看>>
【IScroll深入学习】解决IScroll疑难杂症
查看>>
python 数据类型
查看>>
108-PHP类成员protected和private成员属性不能被查看数值
查看>>
css控制height充满浏览器视口
查看>>
Linux 系统目录结构
查看>>
python学习之 - XML
查看>>
css问题小计
查看>>
Laravel学习笔记(三)数据库 数据库迁移
查看>>
ORACLE查看并修改最大连接数
查看>>
box-flex不均分问题
查看>>