题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160
最大上升子序列的裸题,从给定样例中找到严格满足老鼠体重和速度成反比的尽量多的案例。先按照体重排序,然后在用dp求最大上升子序列(体重是降序排列的),求的时候要注意体重是严格单调的判断。
代码:
#include <bits/stdc++.h>
using namespace std;
struct node {
int ind;
int w,s;
bool operator<(const node& other) const {
if(w == other.w) {
return s < other.s;
}
return w > other.w;
}
};
node mouse[1005];
int dp[1005];
int pre[1005];
int main() {
int n = 1;
while(~scanf("%d%d", &mouse[n].w, &mouse[n].s)) {
mouse[n].ind = n;
n ++;
}
memset(pre, -1, sizeof(pre));
sort (mouse + 1, mouse + n);
// for (int i = 1; i < n; i ++) {
// printf("%d ", mouse[i].ind);
// }
// printf("\n");
int result = 0, resInd = -1;
for (int i = 1; i < n; i ++) {
dp[i] = 1;
for (int j = 1; j < i; j ++) {
if(mouse[i].s > mouse[j].s && mouse[i].w < mouse[j].w) {
if(dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
pre[i] = j;
}
}
}
if(dp[i] > result) {
result = dp[i];
resInd = i;
}
}
printf("%d\n", result);
while(pre[resInd] != -1) {
printf("%d\n", mouse[resInd].ind);
resInd = pre[resInd];
}
printf("%d\n", mouse[resInd].ind);
return 0;
}