码迷,mamicode.com
首页 > 其他好文 > 详细

FJUTOJ-1384-FatMouse's Speed(DP)

时间:2020-04-05 00:45:28      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:ali   class   second   tween   mos   head   att   ssi   return   

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\n",maxx);
    for(int i=1;i<=k;i++)
    {
        if(dp[i]==maxx)
        {
            printf("%d\n",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.

FJUTOJ-1384-FatMouse's Speed(DP)

标签:ali   class   second   tween   mos   head   att   ssi   return   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!