J - 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

题目大意:给你n行,每一行包含重量和速度,要求一个序列的数字满足重量递增,速度递减,给的n行不固定可以改变。

题解:先排序,然后求最长上升子序列。

代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct node
{
    int a,b,num;
}h[100000];

int tt[100000],ii[100000];

int cmp(struct node s,struct node q)
{
    if(s.a==q.a)
        return s.b>q.b;
    else return s.a<q.a;
}

void kk(int num)
{
    if(num==0)
        return ;
    kk(ii[num]);
    printf("%d\n",h[num].num);
}

int main()
{
    int c,d,e;
    c=1;
    while(~scanf("%d %d",&h[c].a,&h[c].b))
    {
        h[c].num=c;
        ++c;
    }
    --c;
    sort(h+1,h+c+1,cmp);
    memset(tt,0,sizeof(tt));
    memset(ii,0,sizeof(ii));
    int ans,v;
    tt[1]=1;
    for(d=2;d<=c;d++)
    {
        ans=0;
        v=0;
        for(e=1;e<d;e++)
        {
            if(h[d].a>h[e].a&&h[d].b<h[e].b&&ans<tt[e])
            {
                ans=tt[e];
                v=e;
            }
        }
        tt[d]=ans+1;
        ii[d]=v;
    }
    ans=0;
    for(d=1;d<=c;d++)
    {
        if(tt[d]>ans)
        {
            ans=tt[d];
            v=d;
        }
    }
    printf("%d\n",tt[v]);
    kk(v);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/The_city_of_the__sky/article/details/81866526
今日推荐