FatMouse's Speed 【HDU - 1160】

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题,根据题意不难看出此题需要用到结构体。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int dp[1010];

struct node
{
    int a,b,c;
} qw[1010];

bool cmp(node xx,node yy)
{
    if(xx.b==yy.b)
        return xx.a<yy.a;
    else
        return xx.b>yy.b;
}

struct note
{
    int x,y;
} s[1010];

int main()
{
    int k=0;
    memset(dp,0,sizeof(dp));
    while(~scanf("%d%d",&qw[k].a,&qw[k].b))
        qw[k].c=k+1,k++;
    int maxx=0;
    sort(qw,qw+k,cmp);//对速度降序排序

    for(int i=0; i<k; i++) //找重量的最长上升子序列
    {
        dp[i]=1;

        for(int j=0; j<i; j++)
            if(qw[i].a>qw[j].a)
                dp[i]=max(dp[i],dp[j]+1);

        if(maxx<dp[i])  //最长长度
            maxx=dp[i];

        s[i].x=0; 
    }
    s[maxx+1].x=0x3f3f3f3f;
    printf("%d\n",maxx);

    for(int i=k-1; i>=0; i--)
        if(qw[i].a<s[dp[i]+1].x)  //如果这个长度的重量小于下一个长度下的重量,才能满足题意
            if(qw[i].a>s[dp[i]].x)  //更新最大值,且保存老鼠的位置
            {
                s[dp[i]].x=qw[i].a;
                s[dp[i]].y=qw[i].c;
            }

    for(int i=1; i<=maxx; i++)
        printf("%d\n",s[i].y);
           return 0;
}

猜你喜欢

转载自blog.csdn.net/Meant_To_Be/article/details/81434018
今日推荐