题目链接: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掉。应该是数据水了吧。

最新文章

  1. 使用ViewSwitcher和ViewFlipper在不同布局中切换
  2. 第几天 switch做法 杭电
  3. 重写String类,也有些区别,供参考
  4. A Game of Thrones(18) - Catelyn
  5. 通过gradle运行测试脚本(转)
  6. 看完这篇文章才对【GIT】有了大彻大悟的认识
  7. Connect By、Level、Start With的使用
  8. jQuery Mobile事件,开发全解+完美注释
  9. Mysql数据库之auto_increment
  10. 利用 pyspider 框架抓取猫途鹰酒店信息
  11. Appium基础知识与环境搭建
  12. shell 脚本实现文件对称加密
  13. linux系统电视盒子到底是什么
  14. iOS runtime实用篇--和常见崩溃say good-bye!
  15. python自学第三天,列表
  16. 使用秘钥ssh登录远程服务器
  17. oracle EBS rtf报表不能输出模板样式
  18. linux中chown命令
  19. 【大数据系列】FileSystem Shell官方文档翻译
  20. H5 canvas控制坦克移动2

热门文章

  1. 生成中文版JavaDoc
  2. ol3简介
  3. javascript实现快速排序和二分法查找
  4. 使用 Struts 2 开发 RESTful 服务
  5. C# WINFORM 捕获全局异常
  6. poj 3537 Crosses and Crosses 博弈论
  7. strrchr函数
  8. hdu 1669(二分+多重匹配)
  9. 以Server模式启动Derby服务竟然抛套接字权限异常
  10. linux c first