题目链接: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)的更多相关文章

  1. HDU 1160 FatMouse's Speed(DP)

    点我看题目 题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置. 思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题 ...

  2. HDU 1160 FatMouse's Speed ——(DP)

    又是那个lis变形的题目. 但是不好定义严格的比较符号,因此只能n^2去做.值得注意的一个是要先排序,因为可能可以先选后面的再选前面的,先排序的话就能够避免这个问题.但是要注意,因为要输出路径,所以要 ...

  3. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  4. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  5. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  8. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  9. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

随机推荐

  1. BZOJ 1029 [JSOI2007] 建筑抢修(贪心)

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 2285  Solved: 1004[Submit][Statu ...

  2. jQuery实现 浏览器后退到上次浏览位置

    近日看腾讯.新浪的移动端网站,发现一件非常蛋疼的事情,在列表浏览内容,我往下翻,往下翻,突然,看到一个十分霸气的标题,于是点到文章查看详细内容,若干时间后,点回退按钮,浏览器回退到页面的最顶部了. 于 ...

  3. C/C++中内存区域划分大总结

    C++作为一款C语言的升级版本,具有非常强大的功能.它不但能够支持各种程序设计风格,而且还具有C语言的所有功能.我们在这里为大家介绍的是其中一个比较重要的内容,C和C++内存区域的划分. 一. 在c中 ...

  4. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  5. Android中的Adapter总结

    一.Adapter的介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for t ...

  6. DF学Mysql(一)——数据库基本操作

    1.创建数据库 create Database <数据库名>; 注意:1)数据库名由字母.下划线.@.#和$组成 2)首字母不能是数字和$符号 3)不允许有空格和特殊字符 2.查看数据库 ...

  7. android开发环境搭建(for 驱动开发人员)

    前言 一.android驱动的开发流程 1: 写LINUX驱动 2: 写LINUX应用测试程序 3: 写JNI接口,用来包装第二步写的应用 (要用NDK来编译) 生成一个.SO文件,相当于CE下的DL ...

  8. Xamarin for Visual Studio 3.11.666 稳定版 破解补丁 Version 3

    前提概要 1.全新安装请参考 安装 Xamarin for Visual Studio. 2.本次补丁包含: ① Xamarin for Visual Studio 3.11.666 ② Xamari ...

  9. [转载] select, poll和epoll的区别

    源地址:http://sheepxxyz.blog.163.com/blog/static/61116213201022003513530/ 随着2.6内核对epoll的完全支持,网络上很多的文章和示 ...

  10. sshpass

    示例: ./sshpass -p ‘123456’  ssh -o StrictHostKeyChecking=no    root@192.168.1.15 ./sshpass -p ‘123456 ...