您的位置:首页 > 其它

HDU 1160 FatMouse's Speed 最长上升子序列及记录路径DP

2013-05-22 16:20 513 查看
点击打开链接
 

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6861    Accepted Submission(s): 2996
Special Judge


[align=left]Problem Description[/align]
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.

 

 
[align=left]Input[/align]
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.

 

 
[align=left]Output[/align]
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
then it must be the case that

   W[m[1]] < W[m[2]] < ... < W[m
]

and

   S[m[1]] > S[m[2]] > ... > S[m
]

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.

 

 
[align=left]Sample Input[/align]

6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

 

 
[align=left]Sample Output[/align]

4
4
5
9
7

 

 
[align=left]Source[/align]
Zhejiang University Training Contest 2001
 

 
[align=left]Recommend[/align]
Ignatius
[align=left] [/align]
[align=left]此题甚是奇葩,要想测试样例还需自己在输入的时候改成这样while(scanf("%d%d",&mouse[cnt].weight,&mouse[cnt].speed)!=EOF&&mouse[cnt].weight)。然后改成这样之后测试样例的结果最后一个7便会变成8;一开始以为是错了,改了一上午也没改对。其实是正确的,因为你在输入的时候会多输入一组测试样例。[/align]
[align=left]先说说题意:找到一个最多的老鼠序列,使得序列中的老鼠的体重满足递增,相应老鼠的速度满足递减。[/align]
思路就是:
思路就是:先按体重递增进行sort排序,然后按照体重找到最长递减子序列即可,用动态规划做比较简单。状态f[i]表示前i个老鼠中的最长递减子序列长度,状态转移方程为f[i] = max{f[j], mice[j].speed > mice[i].speed} + 1, 最后找出最大的f[i]即可。注意此题还需要输出找到的序列中的老鼠的最原始的标号,因此不仅要在刚开始的时候把每个老鼠的最初的序号记下来,还要在进行状态转移的时候把当前的老鼠的位置标记下来。
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int i,j;
int pre[1007],l[1007],ans[1007];//ans记录路径
struct node{
int weight;
int speed;
int num;
}mouse[10007];
int cmp(const node &a,const node &b)
{

if(a.weight!=b.weight)
return a.weight<b.weight;
else
return a.speed>b.speed;

}
int main()
{
int cnt=0;
while(scanf("%d%d",&mouse[cnt].weight,&mouse[cnt].speed)!=EOF&&mouse[cnt].weight)
{
mouse[cnt].num=cnt++;
}
sort(mouse,mouse+cnt,cmp);
for(i=1;i<cnt;i++)
{
pre[i]=0;//记录路径
l[i]=1;//记录长度
//printf("%d\n",mouse[i].weight);
}
for(i=1;i<cnt;i++)
{
for(j=1;j<i;j++)
{
if(mouse[i].weight>mouse[j].weight&&mouse[i].speed<mouse[j].speed)
{
if(l[i]<l[j]+1)
{
l[i]=l[j]+1;
pre[i]=j;
}
}
}
}
int end=1,max=l[1];
for(i=2;i<cnt;i++)
{
if(max<l[i])
{
max=l[i];
end=i;
}
}
printf("%d\n",max);
for(i=1;i<cnt;i++)
{
ans[i]=end;
end=pre[end];
}
for(i=max;i>=1;i--)
{
//printf("%d %d\n",mouse[ans[i]].weight,mouse[ans[i]].speed);
printf("%d\n",mouse[ans[i]].num+1);
}

//printf("%d\n",mouse[ans[1]].num);
return 0;
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  最长上升子序列