YT05-动态归划求解课后题目-1001—FatMouse's Speed-(6.21日-烟台大学ACM预备队解题报告)

FatMouse's Speed

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 12
Special Judge

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Zhejiang University Training Contest 2001 


问题大意:

  一只胖老鼠认为越胖的老鼠跑的越快。当然这是不科学的,所以我们就要拿出证据来反驳他。

输入:

  我们将会输入若干行每行2个整数第一个代表老鼠的体重,第二个代表老鼠的速度。最多输入1000只老鼠的信息,且体重和速度都在1~1000。

Ps:每只老鼠可以有相同的体重,相同的速度,甚至完全一样。

输出:

  输出n在输出n个数,每个数代表第几只老鼠,如第一个从键盘输入的老鼠参数就是1,第6个输入的老鼠参数就是6,n代表这个子列的长度。

  这n个数代表一个有规律的数列,规则是体重递减而速度递增,我们求的就是最长的这么一个数列。

Ps:答案不唯一,因为最长的子列可以有多个。比如本题给的样本数据你输出4 4 5 9 8也是对的。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define MAXN 10050
using namespace std;
int dp[MAXN];
int out[MAXN];
struct mice
{
    int weight;
    int speed;
    int num;
} m[MAXN];
int cmp(mice a,mice b)
{
    if(a.weight!=b.weight)
        return a.weight>b.weight;
    return a.speed<b.speed;
}
int main()
{
    int n,i,j,x,max,max2,end;
    max=0;
    memset(dp,0,sizeof(dp));
    i=1;
    while(scanf("%d%d",&m[i].weight,&m[i].speed)!=EOF)
    {
        m[i].num=i;
        i++;
    }
    n=i;
    sort(m+1,m+n,cmp);
    for(i=1; i<=n-1; i++)
    {
        max2=0;
        for(j=i-1; j>=1; j--)
        {
            if((m[j].weight>m[i].weight)&&(m[j].speed<m[i].speed))
            {
                if(max2<dp[j])
                {
                    max2=dp[j];
                    x=j;
                }
            }
        }
        if(max2)out[m[i].num]=m[x].num;
        dp[i]=max2+1;
        if(dp[i]>max)
        {
            max=dp[i];
            end=m[i].num;
        }
    }
    printf("%d\n",max);
    while(out[end]!=end)
    {
        printf("%d\n",end);
        end=out[end];
    }
    printf("%d\n",end);
    return 0;
}



你可能感兴趣的:(C++)