YT 05-動的帰画解題課後テーマ-1001-FatMouse's Speed-(6.21日-煙台大学ACM予備隊解題報告)

3744 ワード

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 
質問内容:
  1匹の太ったネズミは太ったネズミほど速く走ると思っている.もちろんこれは科学的ではないので、私たちは証拠を出して彼に反論しなければなりません.
入力:
  いくつかの行に2つの整数を入力します.最初はマウスの体重を表し、2番目はマウスの速度を表します.最大1000匹のネズミの情報を入力し、体重も速度も1~1000.
Ps:マウスごとに同じ体重、同じ速度、完全に同じことができます.
出力:
  出力nは、キーボードから最初に入力されたマウスパラメータが1であり、6番目に入力されたマウスパラメータが6であり、nはこのサブ列の長さを表すn個の数を出力する.
  このn個の数は規則的な数列を表し、規則は体重が減少して速度が増加し、私たちが求めているのは最も長い数列である.
Ps:答えは唯一ではありません.一番長いサブ列は複数ありますから.例えば、本題で与えられたサンプルデータは、4 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
",max); while(out[end]!=end) { printf("%d
",end); end=out[end]; } printf("%d
",end); return 0; }