FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23350    Accepted Submission(s): 10391
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
题意::给你许多组数据,每一组两个数,一个代表老鼠的重量,一个代表老鼠的速度,为了证明老鼠越重速度越慢,让你取出几组数据证明,问最多能取出几组。
 
思路:定义dp[i],表示以第i个数据结尾,符合题目要求的序列长度为dp[i],开一个pre数组,用来记录上一个符合要求的数据的下标,再开一个ans数组记录排序之后符合要求的数据的下标,
初始时让p[i].num=i,输出时用ans数组将排序前后的数据下标统一起来输出答案
 
 
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int w;
int v;
int num;
}p[];
bool cmp(node a,node b)//重量递增,速度递减
{
if(a.w<b.w)
return a.w<b.w;
else if(a.w==b.w&&a.v>b.v)
return a.v>b.v;
else
return ;
}
int dp[],pre[],ans[];
//dp[i]表示以第i个数据结尾的符合要求的子序列长度
//pre[i]记录i对应的上一个数据的下标(滚动数组,这里的上一个是指符合要求的数据,所以,pre[i]!=dp[i-1])
//ans[i]存放符合要求的数据的下标(排序之后的下标,输出的时候要对应回去)
int main()
{
int i=;
while(~scanf("%d%d",&p[i].w,&p[i].v))
{
p[i].num=i;
dp[i]=;
pre[i]=-;
i++;
}
int n=i,len=,x;//len最长序列长度,x最长序列长度的下标
sort(p,p+n,cmp); for(int i=;i<n;i++)
{
for(int j=;j<i;j++)
{
if(p[i].w>p[j].w&&p[i].v<p[j].v&&dp[j]+>dp[i])
{
dp[i]=dp[j]+;
pre[i]=j;
if(dp[i]>len)
{
len=dp[i];
x=i;
}
}
}
}
int k=;
while(x!=-)
{
ans[k++]=x;
x=pre[x];
}
printf("%d\n",k);//输出最长序列长度
for(int i=k-;i>=;i--)
{
printf("%d\n",p[ans[i]].num+);
}
return ;
}

hdu 1160 上升序列 dp的更多相关文章

  1. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  2. FatMouse's Speed HDU - 1160 最长上升序列, 线性DP

    #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> usi ...

  3. 怒刷DP之 HDU 1160

    FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  4. HDU 1160 FatMouse's Speed (DP)

    FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  5. HDU 1160 FatMouse's Speed(要记录路径的二维LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  7. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  8. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:让按钮看起来像个链接 (仍然保留按钮行为)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. vue的高级使用技巧

    全局组件注册 一般组件应用弊端,比较笨拙繁琐低效,比如我们写了一些组件,需要引用上的时候就通过import导入,那如果是高频繁需要使用的组件,则需要在每个使用的时候都需要引入并注册 假设现在有两个组件

  3. 28 最小的K个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,.   思路: 解法1:对于小规模数据,可以采用类似前题的快速排序思路 ...

  4. Github版本控制系统

    Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). 特别推荐简单易懂的廖雪锋大神制作的学习教程: https://www.liaoxuefeng.com/wiki/896043 ...

  5. Python运行目录或压缩文件

    如果你在你的工程文件根目录下放一个__main__.py文件,你就可以使用根目录运行整个程序. 你的工程像这样 ex. app/ |-test.py |-test2.py |-__main__.py ...

  6. 七:日期类Date、日期格式化SimpleDateFormat、日历Calendar

    日期的格式转换:

  7. STM32学习笔记:IIC通信协议详解(附带软件模拟源码)

    什么是IIC(I2C)? IIC 即Inter-Integrated Circuit(集成电路总线),这种总线类型是由飞利浦半导体公司设计出来的一种简单.双向.二线制.同步串行总线.它是一种多向控制总 ...

  8. JS+ES6 - 向数组的开头添加一个或更多元素

  9. WARN No appenders could be found for logger 。。。。

    对于类似与标题的警告信息,一般来说是环境在没有加载log4j的配置文件之前就读取了log4j的包. 解决方法就是先加载log4j的配置文件,然后再加载log4j的包. 另一个解决方案就是移除log4j ...

  10. Manjaro Linux 添加源及输入法

    生成可用的中国镜像站列表 sudo pacman-mirrors -i -c China -m rank 勾选相应的镜像站 ,看自己的喜好 如中科大:http://mirrors.ustc.edu.c ...