hdu 1160 上升序列 dp
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
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.
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.
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.
#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的更多相关文章
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- FatMouse's Speed HDU - 1160 最长上升序列, 线性DP
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> usi ...
- 怒刷DP之 HDU 1160
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 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)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- Linux命令:history命令历史的管理及用法
bash可以保存的过去曾经执行过的命令.当某个用户登录到shell中,会读取该用户家目录中的~/.bash_history文件,并将历史命令列表保存到内存中.当用户退出当前shell时,会将内存中的历 ...
- img标签无法显示src中名字中带有中文的图片的问题
img: <img src="/upload/${good.photo}" style="width: 120px;height: 120px;" alt ...
- leetcode206 Reverse Linked List
""" Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL ...
- git客户端出现libpng warning: iCCP: known incorrect sRGB profile
在关闭gitk窗口的时候,会出现一系列的 libpng warning: iCCP: known incorrect sRGB profilelibpng warning: iCCP: known ...
- Ternsorflow 学习:000-在 Ubuntu 16.04 上安装并使用 TensorFlow_v1.14 (改)
声明:本人已经对原文链接根据情况做出合理的改动. 本系列文章使用的是Tensorflow v1.14 引言 TensorFlow 是由谷歌构建的用于训练神经网络的开源机器学习软件.TensorFlow ...
- Activity的生命周期及协同作用
生命周期 onCreate:创建Activity的实例对象的方法 onStart:启动当前的activity实例的方法 onResume:如果该方法执行,页面的实例和用户即可以交互 onPause:如 ...
- OpenJ_Bailian - 1088 滑雪(记忆化搜索)
题意:给定一个二维数组,一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小,输出可以滑行的最长区域的长度. 分析:对于每一个点,进行记忆化搜索.若某点可以向四周某几个点滑行,记忆化搜索求出 ...
- 在GNOME开发人员的努力下,Pango 1.44即将问世
早在5月份,Red Hat的Matthias Clasen共同制定了计划,在近年来相当陈旧的情况下,对Pango布局引擎库进行了一些改进. 这项工作将随着Pango1.44版本的发布而实现,看起来它很 ...
- day04-Python运维开发基础(位运算、代码块、流程控制)
# (7)位运算符: & | ^ << >> ~ var1 = 19 var2 = 15 # & 按位与 res = var1 & var2 " ...
- Nifi简介及核心概念整理
简介 Apache NiFi 是一个易于使用.功能强大而且可靠的数据拉取.数据处理和分发系统,用于自动化管理系统间的数据流. 它支持高度可配置的指示图的数据路由.转换和系统中介逻辑,支持从多种数据源动 ...