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. 

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
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
const int maxn=100005;
struct node
{
    int W;//升序
    int S;//降序
    int point;//下标
}num[maxn];

int DP[maxn];//截止到第i个数,最长单调递增子序列的长度
int path[maxn];//第i个数的上一个数是第path[i]个数
int result[maxn];//存储结果序号
bool cmp(node x,node y)
{
    if(x.S==y.S)
        return x.W<y.W;
    return x.S>y.S;
}
int main()
{
    int len=1;//数据数量
    while(scanf("%d%d",&num[len].W,&num[len].S)!=EOF)
    {
        num[len].point=len;
        len++;
    }

    sort(num+1,num+len+1,cmp);//按照S降序排列
    memset(path,-1,sizeof(path));

    for(int i=1;i<len;i++)
        DP[i]=1;
    //寻找
    for(int i=1; i<len; i++)
    {
        for(int j=1; j<i; j++)
        {
            if(num[j].W<num[i].W&&num[j].S>num[i].S&&DP[j]+1>DP[i])
            {
                DP[i]=DP[j]+1;
                path[i]=j;
            }
        }
    }
    //最长单调递增子序列
    int point=1;
    for(int i=1; i<len; i++)
        if(DP[i]>DP[point])
            point=i;

    //回溯路径
    int len_result=0;
    for(int i=point;i!=-1;i=path[i])
        result[len_result++]=num[i].point;
    //输出路径长度
    printf("%d\n",DP[point]);
    //输出路径
    for(int i=len_result-1;i>=0;i--)
        printf("%d\n",result[i]);

    return 0;
}

 

全部评论

相关推荐

牛客410815733号:这是什么电影查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务