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),找出单调递增最长子序列,并求出其长度. ...
随机推荐
- .Net相关
Lucene 全文搜索 http://lucenenet.apache.org/ Memcached 分布式缓存 http://memcached.org/ selenium UI自动化测试 http ...
- [CF#250 Div.2 D]The Child and Zoo(并查集)
题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...
- Rectangles Area Sum
#include<iostream> #include<stdio.h> #include<math.h> #include<string.h> #in ...
- Vware Workstation pro 12|虚拟机
Vmware是比较不错的PC虚拟化软件,vmware11+不在支持32的系统安装!体积比之前小了很多 VMware 12 官方中文页面 http://vmware.com/cn/products/wo ...
- .net 使用memcache做缓存
前段时间去一家公司面试,面试官问到我对缓存了解多少,因为我是做B/S开发的,所以把知道的都说了.比如:Application.Cache.页面缓存.文件缓存.然后面试官说“不止这些,还有呢?”,我后来 ...
- Java面试常考知识点
1. 什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. Jav ...
- Easyui数据表格-地区列表及工具栏增删改
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- EPUB书籍阅读器插件分享
本文主要分享EPUB文件的打开方式, 包括如何使用火狐浏览器打开epub, 如何使用chrome打开epub, 如何使用IE浏览器打开epub文件: 1:使用火狐打开epub文件 如果有安装火狐浏览器 ...
- liunx下tomcat启动 Cannot find ./catalina.sh
执行启动tomcat命令./startup.sh 提示 Cannot find ./catalina.sh The file is absent or does not have execute pe ...
- 给<tr>标签添加边框
今天做网站时发现给<tr>添加border无效.起初还以为tr不支持这个属性,百度,原来只要给<table>添加一句代码就可以了,代码如下: table{border-coll ...