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
Source
Zhejiang University Training Contest 2001
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160
题目大意是,告诉你老鼠的体重和速度,求一个序列,这个序列是速度递减,体重递增的序列,使其尽可能的长。
Recommend
Ignatius
先是n^2的算法,根据pre回溯;
#include <iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int pos[1005],dp[1005];
struct p //pre记录最长序列的前一个,pos记录输入时的下表,防止sort后乱序
{
int w,sp;
int pre,pos;
}a[1005];
bool cmp(p u,p v)
{
return u.sp>v.sp;
}
void print(int k)
{
if(a[k].pre==-1)
{
printf("%d\n",a[k].pos);return ;
}
else
print(a[k].pre);
printf("%d\n",a[k].pos);
return ;
}
int main()
{
int n=1;
while(~scanf("%d%d",&a[n].w,&a[n].sp))
{
a[n].pre=-1;
dp[n]=1;
a[n].pos=n; //记录下标
n++;
}n--;
sort(a+1,a+n+1,cmp); //先将 速度从大到小排序;然后再求体重的最长上升子序列
int maxx=-1,biao=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
if(a[i].w>a[j].w&&a[i].sp<a[j].sp&&dp[i]<dp[j]+1)
{
dp[i]=dp[j]+1;
a[i].pre=j; //记录前一个下标
if(maxx<dp[i]) //如果更新的maxx则 标记 biao=i 此时biao记录的为最长的序列
{ //再根据pre 回溯
maxx=dp[i];
biao=i;
}
}
}
}
cout << maxx<< endl;
print(biao);//回溯输出
return 0;
}
用lowe_bound()nlogn的方法,求最长子序列;
原理:先赋dp数组最大值,从dp中找大于等于a[i]的数将a[i]放入,即使dp中的元素尽可能小,使a[i]后面的数字可以尽可能多的放入dp数组中,使dp更长;
用一个数组pos标记输入时的位置在dp数组中的位置,当pos【i】==len时即i为最长子序列的一员; 一般 如果是 后面更小的数,进入了dp数组的话,他的pos[i]会小于len;
比如 a数组 1,6,3,5,2
dp 1,6,——变为1,3——1,3,5——1,2,5长度为3,所以最长序列为3,但其存储的值并不是其序列;这就用到pos数组
pos 1==1,2==2,3==2,4==3,5==2; pos[i]为在dp中的位置,i为在a中的位置;
这里必须从后往前遍历,因为有可能在后面的数字比前面的小,而覆盖了前面的数,所以
pos[5]!=len(这里len为3) 然后i--,len不变; pos[4]==len 故 最后将 1,3,4记录到新数组ans中,a[1],a[3],a[4],再讲ans输出即打印了原本路径;
下面是ac 代码,两个代码都ac了;
#include <iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
#define inf 0x3f3f3f3f
int po[1005],dp[1005];
struct p
{
int w,sp;
int pos;
}a[1005];
bool cmp(p u,p v)
{
return u.sp>v.sp;
}
int main()
{
int n=1;
while(~scanf("%d%d",&a[n].w,&a[n].sp))
{
po[n]=n;
a[n].pos=n;
n++;
}n--;
memset(dp,inf,sizeof(dp));
sort(a+1,a+n+1,cmp);
int len=1;
for(int i=1; i<=n; i++)
{
if(a[i].w>dp[len])
{
dp[++len]=a[i].w;
po[i]=len;
}
else
{
int p=lower_bound(dp+1,dp+1+len,a[i].w)-dp;
dp[p]=a[i].w;
po[i]=p;
}
}
cout <<len<< endl;
int ans[1005]={0},p=len,maxx=inf;
for(int i=n;i>=1;i--)
{
if(p<1)break;
if(po[i]==p)
{
ans[p--]=i;
//maxx=a[i].w;
}
}
for(int i=1;i<=len;i++)
printf("%d\n",a[ans[i]].pos);
//print(biao);
return 0;
}