FatMouse's Speed
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.
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.
#include<stdio.h>
#include<iostream>
#include<fstream>
using namespace std;
int mm[10005];
int px[10005];
struct lmx{
int s;
int e;
int dex;
int before;
};
lmx lm[10005];
int cmp(void const *s1,void const *s2)
{
lmx *p=(lmx*)s1;
lmx *q=(lmx*)s2;
if(p->s==q->s) return q->e-p->e;
else return p->s-q->s;
}
int main()
{
//ifstream cin("out.txt");
int a,b,cnt=0,i,j,pos,k,ma;
while(cin>>a>>b)
{
lm[cnt].s=a;
lm[cnt].e=b;
lm[cnt].dex=cnt;
lm[cnt].before=-1;
cnt++;
}
qsort(lm,cnt,sizeof(lm[0]),cmp);
for(i=0;i<cnt;i++) mm[i]=1;
ma=1;
pos=0;
for(i=1;i<cnt;i++)
{
for(j=0;j<i;j++)
{
if(lm[i].s>lm[j].s&&lm[i].e<lm[j].e)
{
if(mm[j]+1>mm[i])
{
mm[i]=mm[j]+1;
lm[i].before=j;
if(mm[i]>ma){ ma=mm[i];pos=i;}
}
}
}
}
px[0]=lm[pos].dex;
k=1;
while(lm[pos].before!=-1)
{
pos=lm[pos].before;
px[k]=lm[pos].dex;
k++;
}
printf("%d\n",k);
for(i=k-1;i>=0;i--) printf("%d\n",px[i]+1);
return 0;
}