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 ...
随机推荐
- jdbc学习一半的代码
用java连接MySQL的准备工作 1.下载MySQL(了解MySQL的基本语法) 2.下载java的和MySQL的连接 3.在程序中加入2中下载的jar包 写java程序连接数据库的基本步骤: 1. ...
- SPringBootJPA的使用快速开发
一文搞懂如何在 Spring Boot 中正确使用 JPA JPA 这部分内容上手很容易,但是涉及到的东西还是挺多的,网上大部分关于 JPA 的资料都不是特别齐全,大部分用的版本也是比较落后的.另外, ...
- windowsService 程序
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 吴裕雄--天生自然JAVAIO操作学习笔记:字节流与字符流操作
import java.io.* ; public class Copy{ public static void main(String args[]){ if(args.length!=2){ // ...
- 落谷p1325雷达安装(计算几何)
传送门 //p1325雷达安装 //很明显雷达应该安装在海岸线上 //而为了满足一个点被覆盖那在区间[x - sqrt(d ^ 2 - y ^ 2), x + sqrt(d ^ 2 - y ^ 2)] ...
- GetHub上很实用的几个Demo
手机号匹配的正则表达式:https://github.com/VincentSit/ChinaMobilePhoneNumberRegex/blob/master/README-CN.md FEBS- ...
- GNS3 ProxyArp(查看路由器是否具有转发功能)
R2是否可以把R1的数据转发出去,参看http://www.cnblogs.com/qq76211822/p/5129134.html 命令:show ip interface f0/0
- 当3D打影人头”成为黑客的秘密武器,隐私该如何保护?
在<碟中谍>系列电影中,除了超级敬业又帅气的阿汤哥之外,最让人津津乐道的桥段就是用3D打印做出来的"人头".通过这些惟妙惟肖的"人头",阿汤哥完成了 ...
- SpringAOP源码跟踪及学习
Spring 版本 4.3.2 在拿到 Bean 实例以后,会经历一系列的初始化工作,如:工厂回调.init 方法.后处理器在 Bean 初始化前后的处理等,在一般情况下(非 factory-meth ...
- R 读取回归模型的信息
参考博客: http://blog.sina.com.cn/s/blog_8f5b2a2e0101fmiq.html https://blog.csdn.net/huangyouyu523/artic ...