[HDOJ1160]FatMouse's Speed(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160
FatMouse 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.
给一组w s,找出一个最长的序列,使得w是严格单调递增而且s是严格单调递减的。
两种做法,分别是关注w和关注s。
关注w:相当于是在s上求最长下降子序列。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef struct Node {
int w;
int s;
int idx;
}Node; const int maxn = ;
Node mice[maxn];
int n, ans;
int dp[maxn];
int pre[maxn];
int st[maxn];
int top; bool cmp(Node a, Node b) {
if(a.w == b.w) return a.s > b.s;
return a.w < b.w;
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
n = ;
while(~scanf("%d %d", &mice[n].w, &mice[n].s)) {
mice[n].idx = n;
n++;
}
n--;
sort(mice + , mice + n + , cmp);
ans = ;
int pos;
memset(dp, , sizeof(dp));
memset(pre, -, sizeof(pre));
for(int i = ; i <= n; i++) {
dp[i] = ;
for(int j = ; j < i; j++) {
if(dp[i] < dp[j] + &&
mice[i].s < mice[j].s &&
mice[i].w > mice[j].w) {
dp[i] = dp[j] + ;
pre[mice[i].idx] = mice[j].idx;
}
}
if(ans < dp[i]) {
ans = dp[i];
pos = mice[i].idx;
}
}
top = ;
for(int i = pos; ~i; i=pre[i]) st[top++] = i;
printf("%d\n", ans);
while(top) printf("%d\n", st[--top]);
return ;
}
关注s:相当于是在w上求最长上升子序列。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef struct Node {
int w;
int s;
int idx;
}Node; const int maxn = ;
Node mice[maxn];
int n, ans;
int dp[maxn];
int pre[maxn];
int st[maxn];
int top; bool cmp(Node a, Node b) {
if(a.s == b.s) return a.w < b.w;
return a.s > b.s;
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
n = ;
while(~scanf("%d %d", &mice[n].w, &mice[n].s)) {
mice[n].idx = n;
n++;
}
n--;
sort(mice + , mice + n + , cmp);
ans = ;
int pos;
memset(dp, , sizeof(dp));
memset(pre, -, sizeof(pre));
for(int i = ; i <= n; i++) {
dp[i] = ;
for(int j = ; j < i; j++) {
if(dp[i] < dp[j] + &&
mice[i].w > mice[j].w) {
dp[i] = dp[j] + ;
pre[mice[i].idx] = mice[j].idx;
}
}
if(ans < dp[i]) {
ans = dp[i];
pos = mice[i].idx;
}
}
top = ;
for(int i = pos; ~i; i=pre[i]) st[top++] = i;
printf("%d\n", ans);
while(top) printf("%d\n", st[--top]);
return ;
}
注意小心关注w时s相等的情况和关注s时w相等的情况。不知道为什么,关注w时没有注意s相等的代码可以AC但是关注s的时候必须要注意w相等要continue掉。应该是数据水了吧。
[HDOJ1160]FatMouse's Speed(DP)的更多相关文章
- HDU 1160 FatMouse's Speed(DP)
点我看题目 题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置. 思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题 ...
- HDU 1160 FatMouse's Speed ——(DP)
又是那个lis变形的题目. 但是不好定义严格的比较符号,因此只能n^2去做.值得注意的一个是要先排序,因为可能可以先选后面的再选前面的,先排序的话就能够避免这个问题.但是要注意,因为要输出路径,所以要 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
随机推荐
- zip&rar格式压缩包的解压缩
实际工作中例子: 首先需要引入两个jar包: 注意:支持压缩软件4.20及以上版本 (1)压缩包的解压: (2)压缩包的压缩打包 zip格式:
- JS模板引擎 :ArtTemplate (1)
1.为什么需要用到模板引擎 我们在做前端开发的时候,有时候经常需要根据后端返回的json数据,然后来生成html,再显示到页面中去. 例如这样子: var data = [ {text: " ...
- volatile关键字的使用
(简要概括:volatile变量有两个作用:一个是告诉编译器不要进行优化:另一个是告诉系统始终从内存中取变量的地址,而不是从缓存中取变量的值) 一.前言 1.编译器优化介绍: 由于内存访问速度远不及C ...
- 常用的CSSreset整理
说道CSSreset,大家又爱又恨,cssreset好处是,覆盖了浏览器的默认样式,使前端攻城狮能更加精确的添加样式,各个浏览器中的界面效果都相同.可是大量的.固定的CSSreset也给网页加载带来一 ...
- Docker学习资料
官方Docker:https://www.docker.com/ CSDN Docker : http://docker.csdn.net/ Docker中文社区:http://www.docker. ...
- Unity3D开发Windows Store应用程序 注意事项
原地址:http://blog.csdn.net/jbjwpzyl3611421/article/details/12704491 针对最近在移植window store项目中遇到的问题,我整理了官方 ...
- oom日志查看
这通常会触发 Linux 内核里的 Out of Memory (OOM) killer,OOM killer 会杀掉某个进程以腾出内存留给系统用,不致于让系统立刻崩溃.如果检查相关的日志文件(/va ...
- HDU Destroy Transportation system(有上下界的可行流)
前几天正看着网络流,也正研究着一个有上下界的网络流的问题,查看了很多博客,觉得下面这篇概括的还是相当精确的: http://blog.csdn.net/leolin_/article/details/ ...
- Heroku 与 ASP.NET 5
一. Heroku 简单来讲,Heroku是一个支持多种语言.极易部署.多价位可免费的 Pass 平台,通过 Buildpack 搭建语言运行环境, 默认内建的大部分是 Web 开发中较为常见的语言, ...
- Android Grapics图像类体系