链接:HDOJ-1160
Problem Description
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.
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],…, m[n] then it must be the case that
W[m[1]] < W[m[2]] < … < W[m[n]]
and
S[m[1]] > S[m[2]] > … > S[m[n]]
In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7
题目大意:
给出一些老鼠的体重(W)和速度(S)(老鼠编号按输入顺序从1开始编号)
求符合
W[m[1]] < W[m[2]] < … < W[m[n]]
and
S[m[1]] > S[m[2]] > … > S[m[n]]
的最长子序列。
输出 最长子序列的长度 和 任意一个最长子序列。
该题不仅要求最长符合子序列,还要记录路径。
①首先按W或者S排序,确定相对位置,这里按W升序排列(转化为LIS问题)
②再就是求最长符合子序列 (动态规划)
dp[i] = max( dp[x1] , dp[x2] , … , dp[xn] ) + 1
x1,x2,…,xn < i && s[x1],s[x2],…,s[xn] < s[i] && w[x1],w[x2],…,w[xn] != w[i]
就是找到能衔接在 i 前面的最长已有序列,再加上1,即是 dp[i]
③关于记录路径,我所想到的方法是创建向量vector< int > ans [1010] ,来记录每个dp[i]对应的序列,即ans[i];
之后看到了别人的方法,是利用一个int pre[1010]的数组来记录路径,类似于逆向的数组实现链表,这个方法可以更加节省空间。
下面都示范一下:
a. vector存储路径
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
struct mouse
{
int num;int w;int s;
};
bool cmp(mouse a,mouse b)
{
if(a.w!=b.w)return a.w<b.w;elsereturn a.s>b.s;
}
using namespace std;
int main()
{
int x,y,N=0;mouse a[1010];while(scanf("%d%d",&x,&y)!=EOF){
N++;a[N].w=x;a[N].s=y; a[N].num=N;}sort(a+1,a+N+1,cmp);int dp[1010]={
0};vector<int> ans[1010];int i,j;for(i=1;i<=N;i++){
for(j=1;j<i;j++){
if(a[i].s<a[j].s&&a[i].w!=a[j].w) //找到可衔接的dp[j]{
if(dp[i]<dp[j]) //找到最大值并更新{
dp[i]=dp[j];ans[i]=ans[j]; //把dp[j]对应的已有序列整体赋给ans[i]}}}dp[i]+=1;ans[i].push_back(a[i].num); //序列末尾加上a[i]}int ans_i,Max=0;for(i=1;i<=N;i++) //找到最大的dp[i]{
if(dp[i]>Max){
Max=dp[i];ans_i=i;}}printf("%d\n",Max);for(j=0;j<ans[ans_i].size();j++) //输出序列printf("%d\n",ans[ans_i][j]);return 0;
}
b. pre数组记录路径
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
struct mouse
{
int num;int w;int s;
};
bool cmp(mouse a,mouse b)
{
if(a.w!=b.w)return a.w<b.w;elsereturn a.s>b.s;
}
using namespace std;
int main()
{
int x,y,N=0;mouse a[1010];while(scanf("%d%d",&x,&y)!=EOF){
N++;a[N].w=x;a[N].s=y; a[N].num=N;}sort(a+1,a+N+1,cmp);int pre[1010],dp[1010]={
0};int i,j;for(i=1;i<=N;i++){
pre[i]=-1; //-1代表某一个子序列的首个元素,其没有prefor(j=1;j<i;j++){
if(a[i].s<a[j].s&&a[i].w!=a[j].w){
if(dp[i]<dp[j]){
dp[i]=dp[j];pre[i]=j; //i的前面是j}}}dp[i]+=1;}int ans_i,Max=0;for(i=1;i<=N;i++) //找到最大的dp[i]{
if(dp[i]>Max){
Max=dp[i];ans_i=i;}}printf("%d\n",Max);stack<int> ans; //因为利用 pre 遍历是逆序,所以可以用 stack 存储遍历for(i=ans_i;i!=-1;i=pre[i]) //入栈ans.push(a[i].num);while(!ans.empty()) //出栈{
printf("%d\n",ans.top());ans.pop();}return 0;
}