HDU 1160 FatMouse's Speed (LIS)

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9871    Accepted Submission(s): 4374
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
 

 

Source
 

 

Recommend
Ignatius
 
这道题的话,有点蛋疼
首先是输入,这道题的输入不太一样= =
可以看出来这道题就LIS,但是和一般的LIS不一样的是这道题要路径打印
所以说dp方程还是LIS的DP方程:dp[i]=dp[j]+1  (a[j].s>a[i].s && a[i].w!=a[j].w)
还是要好好学一下不同类型问题的路径打印= =
这里用到了pre[]数组来记录转移到当前状态的前一个状态的下标
然后递归查找初始状态,也就是pre[i]=-1;
最后回溯依次输出就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=10000+50;
const int INF=0x3f3f3f3f;
const double EPS=1e-9;
int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}};
int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
struct node
{
    int w;
    int s;
    int index;
    bool operator<(const node A)const
    {
        if(w!=A.w) return w<A.w;
        return s>A.s;
    }
}a[MAXN];
int dp[MAXN],pre[MAXN];
void print(int p)
{
    if(pre[p]!=-1) print(pre[p]);
    printf("%d\n",a[p].index);
}
int main()
{
    int weight,speed,cnt=0;
    while(scanf("%d %d",&weight,&speed)!=EOF)
    {
        a[cnt].w=weight;
        a[cnt].s=speed;
        a[cnt].index=cnt+1;
        cnt++;
    }
    sort(a,a+cnt);
    memset(dp,0,sizeof(dp));
    memset(pre,-1,sizeof(pre));

    dp[0]=1;
    int id,maxn=-INF;
    for(int i=1;i<cnt;i++)
    {
        dp[i]=1;
        for(int j=0;j<i;j++)
        {
            if(a[i].w!=a[j].w && a[i].s<a[j].s)
            {
                if(dp[j]+1>dp[i])
                {
                    dp[i]=dp[j]+1;
                    pre[i]=j;
                }
            }
        }
        if(dp[i]>maxn)
        {
            maxn=dp[i];
            id=i;
        }
    }
    printf("%d\n",dp[id]);
    print(id);
    return 0;
}
View Code

 

posted @ 2015-01-26 16:16  Cliff Chen  阅读(129)  评论(0编辑  收藏  举报