Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Special Judge
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
最长上升子序列+路径记录,建立一个结构体数组储存老鼠的体重、速度和序列,按照体重进行排序,如果严格符合条件,dp++,同时记录下因为哪个序列进行的++(这里我有点不好表达,看代码会更直观些)。
C
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
struct mice{
int weight,speed,id;
}a[1001];
int Max(int a,int b)
{
return a>b?a:b;
}
int cmp(const void *a,const void *b)
{
return ((const struct mice *)a)->weight-((const struct mice *)b)->weight;
}
int b[1001],c[1001],dp[1001];
int main(void)
{
int i,j,length=1,max=0,ID;
int temp_weight,temp_speed,temp;
while(scanf("%d %d",&temp_weight,&temp_speed)!=EOF)
{
a[length].weight=temp_weight;
a[length].speed=temp_speed;
a[length].id=length;
length++;
}
qsort(a+1,length-1,sizeof(struct mice),cmp);
for(i=1;i<length;i++)
{
dp[i]=1;
for(j=1;j<i;j++)
if(a[i].weight>a[j].weight&&a[i].speed<a[j].speed)
{
temp=dp[i];
dp[i]=Max(dp[i],dp[j]+1);
if(temp!=dp[i]) //注意这个鬼地方,卡了我3个WA,Max语句不一定会让dp的值改变,改变的时候才要改变记录的位置
b[i]=j;
}
if(max<dp[i])
{
max=dp[i];
ID=i;
}
}
printf("%d\n",max);
j=1;
while(ID)
{
c[j++]=ID;
ID=b[ID];
}
for(i=j-1;i>=1;i--)
printf("%d\n",a[c[i]].id);
return 0;
}