您的位置:首页 > 其它

[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上求最长下降子序列。

#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掉。应该是数据水了吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: