题意:
输入一组小鼠的体重和速度,要求输出体重从小到大而速度从大到小的数目最多的小鼠的数量和具体序号。
要点:
简单的最长上升子序列,只要先排序再DP即可,就是要记录一下路径最后递归输出就行。
17447680 | 2016-07-06 14:10:34 | Accepted | 1160 | 0MS | 1732K | 958 B | C++ | seasonal |
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{int w, s;int pos;
}mice[1005];
int dp[1005], path[1005];int cmp(node a, node b)
{if (a.w == b.w)return a.s > b.s;return a.w < b.w;
}
void print(int x)//递归输出路径
{if (x == -1)return;print(path[x]);printf("%d\n", mice[x].pos+1);
}
int main()
{int i, j, count = 0;while (scanf("%d%d", &mice[count].w, &mice[count].s)!=EOF){mice[count].pos = count;count++;}sort(mice, mice + count, cmp);for (i = 0; i < count; i++)dp[i] = 1;memset(path, -1, sizeof(path));for(i=0;i<count;i++)for(j=0;j<i;j++)if (mice[i].w > mice[j].w&&mice[i].s < mice[j].s&&dp[i] < dp[j] + 1){dp[i] = dp[j] + 1;path[i] = j;//记录前驱}int ans = -1, num;for(i=0;i<count;i++)if (ans < dp[i]){ans = dp[i];num = i;}printf("%d\n", ans);print(num);return 0;
}