HDOJ-1160 FatMouse's Speed(動態規劃,最長符合子序列)
連結: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升序排列
②再就是求最長符合子序列 (動態規劃)
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;
else
return 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;
else
return 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代表某一個子序列的首個元素,其沒有pre
for(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;
}