FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8881 Accepted Submission(s): 3934
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.
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.
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经典问题之最长有序子序列问题,我做起来超费劲,参考别人的博客做的。回忆做的过程,我只有用恶心来形容.......
资料来源:http://blog.youkuaiyun.com/lawrencesgj/article/details/7881851
/******************************
*
* acm: hdu-1160
*
* title: FatMouse's Speed
*
* time: 2014.6.8-15
*
******************************/
//考察dp 最长有序子序列问题
/*
思路:设Mice[i].W表示第i只老鼠的重量,Mice[i].S表示第i只老鼠的速度。
我们先对Mice进行排序,以W为第一关键字,从小到大,S为第二关键字,从大到小。
设f[i]为Mice[i]至Mice[n]最长的序列长度。
考虑某一个f[i],则有:
f[i] = max(f[i], f[j]+1) (1<=j<i,且Mice[i].W> Mice[j].W,Mice[i].S < Mice[j].S)
其中,初始条件为f[i]=1 (i=1, 2, ..., n)
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1001
typedef struct nice
{
int w; //表示第i只老鼠的重量
int s; //表示第i只老鼠的速度
int sign; //老鼠的序号(第几只老鼠)
} Nice[MAXSIZE];
//最长有序子序列
typedef struct LIS
{
Nice nice; //老鼠
int position; //最大长度位置
int f[MAXSIZE]; //记录Nice数组中,对应位置数据为结尾的最长有序序列长度
int p[MAXSIZE]; //记录Nice数组中,对应位置数据位结尾的前一个数据位置
//因为要输出路径,输出的时候递归就可以了
int len; //固定的最大长度
} LIS;
LIS lis;
int max_t; //最长序列大小
//dp方程
//f[i] = max(f[i], f[j]+1) (1<=j<i,且Mice[i].W> Mice[j].W,Mice[i].S < Mice[j].S)
void dp()
{
int i;
int j;
max_t = 0;
for (i = 0; i < lis.len; i++)
{
for (j = 0; j < i; j++)
{
if (lis.nice[i].w > lis.nice[j].w && lis.nice[i].s < lis.nice[j].s)
{
if (lis.f[j]+ 1 > lis.f[i])
{
lis.f[i] = lis.f[j]+ 1;
lis.p[i] = j;
if (lis.f[i] > max_t)
{
max_t = lis.f[i];
lis.position = i;
}
}
}
}
}
}
//递归从头输出最长子序列
void printLIS(int position)
{
if (lis.p[position] == position)
{
printf("%d\n", lis.nice[position].sign);
return ;
}
printLIS(lis.p[position]);
printf("%d\n", lis.nice[position].sign);
}
int main()
{
int j;
int i = 0;
int k;
lis.position = 0;
//输入数据、初始化
while (~scanf("%d%d", &lis.nice[i].w, &lis.nice[i].s))
{
lis.f[i] = 1;
lis.p[i] = i;
lis.nice[i].sign = i + 1;
i++;
}
lis.len = i;
/*静态测试数据
////////////////////////////
lis.nice[0].w = 6008;
lis.nice[0].s = 1300;
lis.f[0] = 1;
lis.p[0] = 0;
lis.nice[0].sign = 1;
lis.nice[1].w = 6000;
lis.nice[1].s = 2100;
lis.f[1] = 1;
lis.p[1] = 1;
lis.nice[1].sign = 2;
lis.nice[2].w = 500;
lis.nice[2].s = 2000;
lis.f[2] = 1;
lis.p[2] = 2;
lis.nice[2].sign = 3;
lis.nice[3].w = 1000;
lis.nice[3].s = 4000;
lis.f[3] = 1;
lis.p[3] = 3;
lis.nice[3].sign = 4;
lis.nice[4].w = 1100;
lis.nice[4].s = 3000;
lis.f[4] = 1;
lis.p[4] = 4;
lis.nice[4].sign = 5;
lis.nice[5].w = 6000;
lis.nice[5].s = 2000;
lis.f[5] = 1;
lis.p[5] = 5;
lis.nice[5].sign = 6;
lis.nice[6].w = 8000;
lis.nice[6].s = 1400;
lis.f[6] = 1;
lis.p[6] = 6;
lis.nice[6].sign = 7;
lis.nice[7].w = 6000;
lis.nice[7].s = 1200;
lis.f[7] = 1;
lis.p[7] = 7;
lis.nice[7].sign = 8;
lis.nice[8].w = 2000;
lis.nice[8].s = 1900;
lis.f[8] = 1;
lis.p[8] = 8;
lis.nice[8].sign = 9;
i = 9;
lis.len = 9;
/////////////////////////
*/
//w升序排列 (2次冒泡排序)
for (j = 0; j < lis.len - 1; j++)
{
k = j;
for (i = j + 1; i < lis.len; i++)
{
if (lis.nice[k].w > lis.nice[i].w)
{
k = i;
}
}
if (k != j)
{
int temp;
temp = lis.nice[k].w;
lis.nice[k].w = lis.nice[j].w;
lis.nice[j].w = temp;
temp = lis.nice[k].s;
lis.nice[k].s = lis.nice[j].s;
lis.nice[j].s = temp;
temp = lis.nice[k].sign;
lis.nice[k].sign = lis.nice[j].sign;
lis.nice[j].sign = temp;
}
}
//w升序的前提下,s降序排列
for (j = 0; j < lis.len - 1; )
{
k = j;
for (i = j + 1; i < lis.len; i++)
{
if (lis.nice[k].w == lis.nice[i].w)
{
k = i;
}
else
{
break;
}
}
//可以优化成只找最大的 这里没有优化
if (k != j) //j < k j=4 k=6
{
int t;
int u;
int v;
for (t = j; t < k; t++)
{
v = t;
for (u = t + 1; u <= k; u++)
{
if (lis.nice[v].s < lis.nice[u].s)
{
v = u;
}
}
if (v != t)
{
int temp;
temp = lis.nice[v].s;
lis.nice[v].s = lis.nice[t].s;
lis.nice[t].s = temp;
temp = lis.nice[v].sign;
lis.nice[v].sign = lis.nice[t].sign;
lis.nice[t].sign = temp;
}
}
} //end if (k != j)
if (k == j)
{
j++;
}
else
{
j = k;
}
}//end 2次排序
dp();
printf("%d\n", max_t);
printLIS(lis.position);
return 0;
}