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),找出单调递增最长子序列,并求出其长度. ...
随机推荐
- 知乎UWP 预览
又是很久都没有写博客了,为了表达歉意,奉上一个新的App,O(∩_∩)O! 因为商店的知乎太多了,然而,,所以一直打算自己动手写一个. 近段时间有些假期加上课程不是很忙,抽时间写了这个知乎.商店链接 ...
- 系统升级日记(3)- 升级SharePoint解决方案和Infopath
最近一段时间在公司忙于将各类系统进行升级,其最主要的目标有两个,一个是将TFS2010升级到TFS2013,另外一个是将SharePoint 2010升级到SharePoint 2013.本记录旨在记 ...
- C# 利用反射动态将字符串转换成属性对应的类型值
/// <summary> /// 为指定对象分配参数 /// </summary> /// <typeparam name="T">对象类型& ...
- MSSQL 问题集锦
[1]关于SQL Server数据库连接字符串的特殊参数说明 MultipleActiveResultSets和Mars_Connection同义,指定此数据库连接是否复用数据库内已建立的相同用户的连 ...
- JavaScript的理解记录(2)
一.表达式与运算符: 1.对于属性访问表达式: var arr = {first:"hh","second":"gg",third:null ...
- 2-ls 显示目录内容
ls list directory contents 显示目录内容 [语法]: ls [选项] [参数] [功能介绍] ls指令用来显示目录列表,在Linux系统中有着较高的使用率.ls指令的输出信息 ...
- android 调用电话功能
今天用到了打电话的功能,这要如何实现呢? 很简单 1.创建对应对的xml展示页面喝java文件 2.在manifest中添加权限 下面上代码吧: 这是布局的一部分 <LinearLayout a ...
- PHP 异常
<?php /* PHP 异常处理 什么是异常? PHP 5 提供了一种新的面向对象的错误处理方法. 异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程. 这种情况称为异常. 当异常被 ...
- 不停止MySQL服务的情况下修改root的密码
首先我们得知道一个MySQL普通用户的密码 这里我来记录一下我的操作过程 这里我刚刚到一家公司上面装的是cacti,但是之前的运维不记得MySQL的root密码了 但是他知道cacti的密码, 用户: ...
- 【CodeVS 2083】Cryptcowgraphy 解密牛语
http://codevs.cn/problem/2083/ 奶牛搜索题.我加了如下剪枝: 1.用字符串hash判重.注意判重时也要对字符串长度判重,否则会出现两个字符串长度不同但hash值相同的情况 ...