FatMouse's Speed (hdu 1160)

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 <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 1007
#define N 100005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<x<<" "<<endl
using namespace std;
typedef long long ll;

struct mice
{
    int w;
    int s;
    int id;//编号
};
struct node1{
	int pre;//后缀
	int num;//个数
}dp[1111];
mice mices[1001];
int cmp(struct mice a,struct mice b)
{
    if(a.w!=b.w)
        return a.w<b.w;
    else
        return a.s>b.s;
}
int main()
{
    int i=0;
    while(scanf("%d %d",&mices[i].w,&mices[i].s)!=EOF)
    {
        mices[i].id=i+1;
        i++;
        //if(i==9)
         //   break;
    }
    int n=i,max=1,t=1;
    for(int i=0;i<n;i++)//初始化,使num都大于0
	{
		dp[i].num=1;
		dp[i].pre=0;
	}
    sort(mices,mices+n,cmp);
    for(int i=1; i<n; i++)
        for(int j=0; j<i; j++)
        {
            if(mices[j].w<mices[i].w&&mices[j].s>mices[i].s)
            {
                if(dp[i].num<dp[j].num+1)
                {
                    dp[i].num=dp[j].num+1;
                    dp[i].pre=j;
                }
            }
            if(dp[i].num>max)
            {
                max=dp[i].num;
                t=i;
            }
        }
    cout<<max<<endl;
    int m[1111];
    for(int j=1;j<=max;j++)
	{
		m[j]=t;
		t=dp[t].pre;
	}
	for(int j=max;j>=1;j--)
		printf("%d\n",mices[m[j]].id);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/baiyi_destroyer/article/details/81198765