Problem Description
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 <stdio.h>
#include <stdlib.h>
using namespace std;
struct mice
{
 int weight;
 int speed;
 int key;
}a[1010];
int cmp(const void *a,const void *b)
{
 struct mice *c=(mice *)a;
 struct mice *d=(mice *)b;
 if(c->weight!=d->weight)
  return c->weight-d->weight;
 else
  return d->speed-c->speed;
}
int main()
{
 int w,s,k=-1,i,j;
 int dp[1010],pre[1010],rec[1010];//dp[i]表示以第i个数据结尾的符合要求的子列长度
 while(scanf("%d%d",&w,&s)!=EOF)//pre[i]记录i对应的上一个数据,rec[i]存放最终结果下标
 {
  k=k+1;
  a[k].weight=w;
  a[k].speed=s;
  a[k].key=k+1;
 }
 qsort(a,k+1,sizeof(a[0]),cmp);
 for(i=0;i<=k;i++)
 {
  dp[i]=1;
  pre[i]=0;
 }
 int maxi,maxlen=0;
 for(i=0;i<=k;i++)
  for(j=0;j<i;j++)
   if(a[i].weight>a[j].weight&&a[i].speed<a[j].speed&&dp[i]<dp[j]+1)
   {
    dp[i]=dp[j]+1;
                pre[i]=j;
    if(dp[i]>=maxlen)
    {
     maxlen=dp[i];
     maxi=i;
    }
   }
 i=0;
    while(maxi!=0)
 {
  rec[i]=maxi;
  maxi=pre[maxi];
  i++;
 }
 cout<<i<<endl;
 while(i>0)
 {
  i--;
  cout<<a[rec[i]].key<<endl;
 }
 return 0;
}
posted on 2014-10-31 17:05  星斗万千  阅读(160)  评论(0编辑  收藏  举报