YT05-動態歸劃求解課後題目-1001—FatMouse's Speed-(6.21日-煙臺大學ACM預備隊解題報告)

kewlgrl發表於2015-07-15

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;
}



相關文章