HD1160FatMouse's Speed(最长单调递增子序列)
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13194 Accepted Submission(s): 5807
Special Judge
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.
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.
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
4
5
9
7
题意:输入若干个mice的weight和speed,然后要求weight递增和speed递减的最长序列,然后输出分别是那几个
卡死了被这题,突然最长单调递增都不会写了=_=
将speed按照递减排序自后就是找weight的单调递增了...只不过还有记录一下是由那个推过来的,本来想写链表结果不会写了=_=,用一个数组记录也行,找到当前这个是由哪一个递推过来的,然后呢..其实这就是倒叙了已经,然后我却直接输出了wa了一番,因为忘记了+1,因为数组是从0开始的,弄得都混乱了,然后又浪费了20分钟的查错。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = + ;
int n;
int dp[N];
vector<int> id_set[N];
struct Mouse
{
int weight, speed;
int id;
};
Mouse mice[N];
int cmp(Mouse x, Mouse y)
{
return x.speed > y.speed;
}
void solve()
{
memset(dp, , sizeof(dp));
for(int i = ; i <= n; i++)
id_set[i].clear();
dp[] = ;
int last_loca = ;
id_set[].push_back();
int ans = ;
for(int i = ; i <= n; i++)
{
int maxn = , pos = i;
for(int j = i - ; j >= ; j--)
{
if(mice[i].weight > mice[j].weight && maxn < dp[j] && mice[i].speed != mice[j].speed) //严格递减,所以speed不能相等
{
maxn = dp[j];
pos = j;
}
}
dp[i] = maxn + ;
id_set[i].push_back(pos);
if(ans < dp[i])
{
ans = dp[i];
last_loca = i;
}
}
printf("%d\n", ans);
stack<int> temp;
for(int i = ; i < ans; i++)
{
//printf("%d %d %d\n", mice[last_loca].weight, mice[last_loca].speed, mice[last_loca].id);
temp.push(mice[last_loca].id);
last_loca = id_set[last_loca][];
}
while(!temp.empty())
{
int tempx = temp.top();
temp.pop();
printf("%d\n", tempx + );
}
}
int main()
{
n = ;
while(scanf("%d%d", &mice[n].weight, &mice[n].speed) != EOF)
{
mice[n].id = n;
n++;
}
sort(mice, mice + n, cmp);
// cout << "--------------" << endl;
//for(int i = 0; i < n; i++)
// cout << mice[i].id << " " << mice[i].weight << " " << mice[i].speed << endl;
solve();
return ;
}
HD1160FatMouse's Speed(最长单调递增子序列)的更多相关文章
- 动态规划-最长单调递增子序列(dp)
最长单调递增子序列 解题思想:动态规划 1.解法1(n2) 状态:d[i] = 长度为i+1的递增子序列的长度 状态转移方程:dp[i] = max(dp[j]+1, dp[i]); 分析:最开始把d ...
- [C++] 动态规划之矩阵连乘、最长公共子序列、最大子段和、最长单调递增子序列、0-1背包
一.动态规划的基本思想 动态规划算法通常用于求解具有某种最优性质的问题.在这类问题中,可能会有许多可行解.每一个解都对应于一个值,我们希望找到具有最优值的解. 将待求解问题分解成若干个子问题,先求解子 ...
- [dp]最长单调递增子序列LIS
https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...
- poj1631Bridging signals(最长单调递增子序列 nlgn)
Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12251 Accepted: 6687 ...
- NYOJ17 最长单调递增子序列 线性dp
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=17 分析: i=1 dp[i]=1 i!=1 dp[i]=max(dp[j]+1) ...
- nyoj 单调递增子序列(二)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...
- nyist oj 214 单调递增子序列(二) (动态规划经典)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 ,a2...,an}(0<n<=100000).找出单调递增最长子序列,并求出其长度 ...
- ny214 单调递增子序列(二) 动态规划
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...
- nyoj 214 单调递增子序列(二)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 ,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度. ...
随机推荐
- Mecanim动画模型规范
面数控制, 以三角面计算 不要超过4边的面 光滑组,法线 单位CM,单位比例 中心点 3DMax:Reset Transform Maya:Freeze Transformation 帧率:30帧 不 ...
- 正则表达式语法(msdn)
“正则表达式”描述在搜索文本正文时要匹配的一个或多个字符串.该表达式可用作一个将字符模式与要搜索的字符串相匹配的模板. 正则表达式包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符” ...
- 安装.NET Framework后程序无法启动的错误处理
最近发现一直在使用的Database.NET软件无法正常使用了,表现为当尝试进行Sql Server的连接创建时,直接报错 在事件查看器具体错误信息为: 日志名称: Applicat ...
- Matlab中给figure添加图例(legend),标题(title)和颜色(color)
在Matlab绘图过程中,尤其是需要将多个图绘制在相同的坐标轴中时,通常需要将不同的曲线设置成为不同的颜色.此外,为了直观,还需要给这张图标增添标题和图例.这篇文章展示了在Matlab的绘图窗口(fi ...
- The Lifecycle and Cascade of WeChat Social Messaging Groups-www2016-20160512
分析性论文: 分析并预测微信群的生命周期,以及群成员的邀请模式. 参考资料:http://www.360doc.com/content/16/0423/11/26166517_553076725.sh ...
- android开发------编写用户界面之相对布局
今天要说的是RelativeLayout.RelativeLayout相对于LinearLayout的主要不同点在于它需要一个参照物. 我们先来看一下官方对这个布局的解释: RelativeLayou ...
- python基础-基本数据类型
一. 运算符 1.算数运算: ps: 示例1: python2.7示例 #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: nulige #算数 ...
- 总有一天会NB的! SB一样的坚持会有NB一样的结果的!
第一天: 1.背景图不显示,因为背景图片无法撑开div,所以必须自己设置div的高度哦! 2.div水平居中! A:margin-left:auto;margin-right:auto; B: 父元 ...
- MyEclipse去除网上复制下来的来代码带有的行号
作为开发人员,我们经常从网上复制一些代码,有些时候复制的代码前面是带有行号,如: MyEclipse本身自带有查找替换功能,并且支持正则表达式替换,使用正则替换就可以很容易去除这些行号 使用快捷键“c ...
- SSM框架搭建(转发)
SSM框架,顾名思义,就是Spring+SpringMVC+mybatis. 通过Spring来将各层进行整合, 通过spring来管理持久层(mybatis), 通过spring来管理handler ...