HDU1160 FatMouse's Speed LIS变种+路径

来源:互联网 发布:linux命令 cp r 编辑:程序博客网 时间:2024/04/30 16:19

J - FatMouse’s Speed

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

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

题解

题目的意思是输入每只老鼠的体重和速度,输出在体重递增的情况下,速度的最长不上升序列的个数和路径,输入完后先按体重递增排序,然后LIS的时候顺便记录路径,最后逆序输出。不难,但很考验细心和耐心…调试了好久快奔溃了..突然冒出来的AC真是太感人了!

代码

#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;struct mice{   int val;                   //最长序列的个数   int weight;   int speed;   int number;                //一开始的编号   int p;                     //路径}m[1005];bool cmp(mice a,mice b){      return a.weight<b.weight;}int main(){   int cnt = 0;   while(scanf("%d%d",&m[cnt].weight,&m[cnt].speed)!=EOF)   {        m[cnt].number = cnt+1;        m[cnt].p = -1;        cnt++;   }   int a[1005];   sort(m,m+cnt,cmp);   for(int i = 0;i<cnt;i++)        for (int j = 0;j<i;j++)        {             if (m[i].speed < m[j].speed && m[i].val<m[j].val+1 && m[i].weight!=m[j].weight)             {                m[i].val = m[j].val+1;                m[i].p = j;             }        }    int maxn = 0;int markmax = 0;    for (int i = 0;i<cnt;i++)        if (m[i].val >= maxn)        {            maxn = m[i].val;            markmax = i;        }    printf("%d\n",maxn+1);    a[0] = m[markmax].number;    int temp = m[markmax].p;    int i = 1;    int flag = 1;    while (flag)    {        if (temp == -1)        {         flag = 0;         break;        }        a[i] = m[temp].number;        temp = m[temp].p;        i++;    }    for (i = maxn;i>=0;i--)        printf("%d\n",a[i]);}
0 0
原创粉丝点击