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 ...
随机推荐
- Prometheus组件
Prometheus组件 上一小节,通过部署Node Exporter我们成功的获取到了当前主机的资源使用情况.接下来我们将从Prometheus的架构角度详细介绍Prometheus生态中的各个组件 ...
- Memcached 最新版本发布,不再仅仅是个内存缓存了
导读 Memcached 1.5.18和之后版本可以在服务重启时恢复内存缓存.新版本还通过DAX文件系统挂载来实现缓存持久性功能. 可以通过在启动选项使用该功能: -e /tmpfs_mount/me ...
- english-phoneme
1. 声音概述 2. 音素phoneme与音标 2.1 音素与音标 2.2 音素与字母 2.3 字母发音-字母自然发音对照表 2.4 音标表 2.5 元音字母-辅音字母表 2.6 单元音发音口形趋势表 ...
- Hadoop入门概念
Hadoop作者:Dong Cutting. 受Google三篇论文的启发. 版本: Apache:官方版本 Cloudera:官方版本的封装,优化,打很多patch,商业版本 HortonWorks ...
- JavaScript(3)---事件冒泡、事件捕获
JavaScript(3)---事件冒泡与事件捕获 一.理解冒泡与捕获 假设有这么一段代码 <body> <div><p>标签</p> </div ...
- FPGA流程设计
做fpga也有四年时间了,该有个总结.刚开始那会,学习东西都是死记硬背,去面试也是直接带着答案去了. 时间久了,才懂得设计一些基本思路. 1. 设计输入: verilog代码和原理图.画原理图都是懒得 ...
- 使用 MYSQLBINLOG 来恢复数据
使用 MYSQLBINLOG 来恢复数据 2009-04-05 12:47:05 标签:mysql mysqlbinlog 恢复 数据库 数据 原创作品,允许转载,转载时请务必以超链接形式标明文章 原 ...
- Socket传输大文件(发送与接收)
下载 Client using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- 【LOJ2127】「HAOI2015」按位或
题意 刚开始你有一个数字 \(0\),每一秒钟你会随机选择一个 \([0,2^n-1]\) 的数字,与你手上的数字进行或操作.选择数字 \(i\) 的概率是 \(p[i]\) . 问期望多少秒后,你手 ...
- 落谷p1325雷达安装(计算几何)
传送门 //p1325雷达安装 //很明显雷达应该安装在海岸线上 //而为了满足一个点被覆盖那在区间[x - sqrt(d ^ 2 - y ^ 2), x + sqrt(d ^ 2 - y ^ 2)] ...