FJUTOJ-1384-FatMouse's Speed(DP)

FatMouse's Speed

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.
SampleInput
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
SampleOutput
4
4
5
9
7


2020-4-4清明节练习赛。这题明显就是需要用到DP,首先需要对输入的值进行排序,输入时记录每个数输入的序列,把体重轻的或者体重一样速度较快的放到前面,进行排序。
之后对每一个数进行枚举,找到每个数的后面有几个是满足条件的,记录下来,找到最大的子序列,DP算法的时间复杂为2^,比较高,需要依次迭代,其实也可以用递归,就是比较不好理解,递归太难了。最后附上我的代码。
/**
rid: 389582
user: 965251545
time: 2020-04-04 16:06:20
result: Accepted 
*/
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=10005;
struct node
{
    int w,s,data;
}s[maxn];
bool cmp(const node &t1,const node &t2)
{
    if(t1.w==t2.w)
        return t1.s>t2.s;
    else
        return t1.w<t2.w;
}
int dp[maxn];
int main(void)
{
    int k=1;
    while(~scanf("%d%d",&s[k].w,&s[k].s))
    {
        s[k].data=k;
        k++;
    }
    sort(s+1,s+k,cmp);
    int maxx=0;
    for(int i=k;i>=1;i--)//最长上升子序列
    {
        dp[i]=1;
        for(int j=i;j<=k;j++)
        {
            if(s[j].w>s[i].w&&s[j].s<s[i].s)//记录往后有几个序列是按照规律的
                dp[i]=max(dp[i],dp[j]+1);
        }
        maxx=max(maxx,dp[i]);
    }
    printf("%d
",maxx);
    for(int i=1;i<=k;i++)
    {
        if(dp[i]==maxx)
        {
            printf("%d
",s[i].data);
            maxx--;
        }
        if(maxx==0)
            break;
    }
}

—————————————————————————————————————————————————————————————————————

The day Kobe won my respect, some teammates complained to me that Kobe didn't pass the ball. I said I went to talk to him.

奥尼尔:Kobe, there's no letter' I 'in the word.

科比:I know, but there's me in the word, asshole.

原文地址:https://www.cnblogs.com/zhaohongjie/p/12635153.html