HDU - 1160 FatMouse's Speed 【DP】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1160
题意
给出一系列的 wi si
要找出一个最长的子序列 满足
wi 是按照升序排列的
si 是按照 降序排列的
思路
因为有双关键词
我们 可以先将一关键词 比如 W 按照 升序排序
再根据 S 关键词 来找 最长下降子序列 就可以了
要输出 其中的一个子序列 我们只要 记录 其 父节点就可以
再循环网上找
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
struct node
{
int w, s;
int id;
}q[maxn];
bool comp(node x, node y)
{
if(x.w == y.w)
return x.s > y.s;
return x.w < y.w;
}
int dp[maxn];
int pre[maxn];
int main()
{
int pos = 1;
while (~scanf("%d%d", &q[pos].w, &q[pos].s))
q[pos].id = pos++;
sort(q, q + pos, comp);
int ans = 1;
CLR(dp, 0);
CLR(pre, 0);
for (int i = 1; i <= pos; i++)
{
dp[i] = 1;
for (int j = 1; j < i; j++)
{
if (q[i].s < q[j].s && q[i].w > q[j].w)
{
if (dp[j] + 1 > dp[i])
{
dp[i] = dp[j] + 1;
pre[i] = j;
}
}
}
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
vector <int> v;
for (int i = 1; i <= pos; i++)
{
if (dp[i] == ans)
{
int vis = i;
v.pb(q[vis].id);
while (pre[vis])
{
v.pb(q[pre[vis]].id);
vis = pre[vis];
}
break;
}
}
vector <int>::iterator it;
for (it = v.end(), it--; ; it--)
{
printf("%d\n", *it);
if (it == v.begin())
break;
}
}
HDU - 1160 FatMouse's Speed 【DP】的更多相关文章
- HDU 1160 FatMouse's Speed LIS DP
http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的 ...
- HDU 1160 FatMouse's Speed (sort + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体 ...
- HDU 1160 FatMouse's Speed(DP)
点我看题目 题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置. 思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题 ...
- hdu 1078 FatMouse and Cheese【dp】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:每次仅仅能走 横着或竖着的 1~k 个格子.求最多能吃到的奶酪. 代码: #include ...
- HDU 1160 FatMouse's Speed ——(DP)
又是那个lis变形的题目. 但是不好定义严格的比较符号,因此只能n^2去做.值得注意的一个是要先排序,因为可能可以先选后面的再选前面的,先排序的话就能够避免这个问题.但是要注意,因为要输出路径,所以要 ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
- 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) ...
- HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- nginx 启动,重启,添加开机启动等相关命令
nginx -t 测试 配置文件是否正确,同时可以查看配置文件路径 nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx ps -ef|grep ng ...
- (原创)lightgbm 一些错误情况处理
1.做多分类问题时候(mutticlass),如果遇到 lightgbm.basic.LightGBMError: b'Number of classes should be specified an ...
- sprinvmvc整合swagger实现实时接口信息展示
1.pom.xml引入swagger插件 <dependency> <groupId>io.springfox</groupId> <artifactId&g ...
- 【音乐App】—— Vue-music 项目学习笔记:用户个人中心开发
前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 歌曲列表 收藏歌曲 一.用 ...
- JSBridge深度剖析
概述 做过混合开发的人都知道Ionic和PhoneGap之类的框架,这些框架在web基础上包装一层Native.然后通过Bridge技术的js调用本地的库. 在讲JSBridge技术之前.我们来看一下 ...
- Android使用OKHttp3实现下载(断点续传、显示运行进度)
OKHttp3是现在很流行的Android网络请求框架,那么怎样利用Android实现断点续传呢,今天写了个Demo尝试了一下,感觉还是有点意思 准备阶段 我们会用到OKHttp3来做网络请求,使用R ...
- 【Excle数据透视】二维数据如何创建数据透视表
二维数据在创建数据透视表的时候,可能会给你带来一些麻烦,没法创建,会丢失维度,那怎么办呢? 解决办法:使用数据透视表和数据透视图向导即可创建 具体操作如下: 按下[Alt+D+P],出现如下界面 选择 ...
- 将VS2010里的红色波浪线去掉
有时候,程序编译没错,却出现了一堆的红色波浪形,看着就烦. 解决方案:在VAssistX菜单栏->Visual Assist X Options->展开Advanced->Under ...
- Siteserver平台搭建
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 一开始什么也不懂真痛 ...
- SQLSERVER 2008 链接 到 ORACLE 11
MSSQL2008R2 链接 ORACLE 11: 创建链接: exec sp_addlinkedserver 'DBLINK_ORACL' , 'ORACLE' , 'MSDAORA' , 'ORC ...