[HDOJ1160]FatMouse's Speed(DP)
2016-04-16 11:49
477 查看
题目链接: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上求最长下降子序列。
View Code
注意小心关注w时s相等的情况和关注s时w相等的情况。不知道为什么,关注w时没有注意s相等的代码可以AC但是关注s的时候必须要注意w相等要continue掉。应该是数据水了吧。
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 = 1111; 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 = 1; while(~scanf("%d %d", &mice .w, &mice .s)) { mice .idx = n; n++; } n--; sort(mice + 1, mice + n + 1, cmp); ans = 0; int pos; memset(dp, 0, sizeof(dp)); memset(pre, -1, sizeof(pre)); for(int i = 1; i <= n; i++) { dp[i] = 1; for(int j = 1; j < i; j++) { if(dp[i] < dp[j] + 1 && mice[i].w > mice[j].w) { dp[i] = dp[j] + 1; pre[mice[i].idx] = mice[j].idx; } } if(ans < dp[i]) { ans = dp[i]; pos = mice[i].idx; } } top = 0; for(int i = pos; ~i; i=pre[i]) st[top++] = i; printf("%d\n", ans); while(top) printf("%d\n", st[--top]); return 0; }
View Code
注意小心关注w时s相等的情况和关注s时w相等的情况。不知道为什么,关注w时没有注意s相等的代码可以AC但是关注s的时候必须要注意w相等要continue掉。应该是数据水了吧。
相关文章推荐
- tango with django ch7 MTV
- Glide框架实现图片加载和视频播放总结
- JavaScript模拟百度分页
- 复杂链表的复制
- Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值
- @protocol和category中如何使用@property?
- linux/ALSA SRC重采样造成的问题及解决
- Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值
- HDU 2083
- ios多线程操作(一)—— 多线程基础与原理
- 隐式类型转换
- 【Unity】第6章 Unity脚本开发基础
- NSIS Error:Error writing temporary file. Make sure your temp folder is valid的解决
- 15 个非常棒的 CSS3 效果教程
- Spring中的AOP理解
- Hdu 1429 胜利大逃亡(续)【bfs+状态压缩】
- python学习笔记(threading多线程)
- RecyclerView的使用学习
- mysql innodb 引擎 key - 和 Primary key的区别 以及 索引的用法总结
- 矩阵问题总结