您的位置:首页 > 其它

HDU 1160 FatMouse's Speed--经典DP

2014-03-24 20:19 218 查看

FatMouse'sSpeed

TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)

TotalSubmission(s):4910AcceptedSubmission(s):2130

SpecialJudge


ProblemDescription

FatMousebelievesthatthefatteramouseis,thefasteritruns.Todisprovethis,youwanttotakethedataonacollectionofmiceandputaslargeasubsetofthisdataaspossibleintoasequencesothattheweightsareincreasing,butthespeedsaredecreasing.

Input

Inputcontainsdataforabunchofmice,onemouseperline,terminatedbyendoffile.

Thedataforaparticularmousewillconsistofapairofintegers:thefirstrepresentingitssizeingramsandthesecondrepresentingitsspeedincentimeterspersecond.Bothintegersarebetween1and10000.Thedataineachtestcasewillcontaininformation
foratmost1000mice.

Twomicemayhavethesameweight,thesamespeed,oreventhesameweightandspeed.

Output

Yourprogramshouldoutputasequenceoflinesofdata;thefirstlineshouldcontainanumbern;theremainingnlinesshouldeachcontainasinglepositiveinteger(eachonerepresentingamouse).Ifthesenintegersarem[1],m[2],...,m
thenitmust
bethecasethat

W[m[1]]<W[m[2]]<...<W[m
]

and

S[m[1]]>S[m[2]]>...>S[m
]

Inorderfortheanswertobecorrect,nshouldbeaslargeaspossible.

Allinequalitiesarestrict:weightsmustbestrictlyincreasing,andspeedsmustbestrictlydecreasing.Theremaybemanycorrectoutputsforagiveninput,yourprogramonlyneedstofindone.

SampleInput


60081300600021005002000100040001100300060002000800014006000120020001900

SampleOutput


44597

最长有序子序列的问题

从后向前DP。

设g[I].W表示第i只老鼠的重量,g[i].S表示第i只老鼠的速度。我们先对Mas进行排序,以W为第一关键字,从小到大,S为第二关键字,从大到小。
设f[i]为g[i]至g
最长的序列长度。考虑某一个f[i],则有:
f[i]=max(f[i],f[j]+1)(1<=j<i,且g[i].W>g[j].W,g[i].S<g[j].S)

#include<string.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
usingnamespacestd;

structmas
{
intweigt;
intsped;
intkey;
};

boolcmp(masx,masy)
{
if(x.weigt!=y.weigt)
returnx.weigt<y.weigt;
else
returnx.sped>y.sped;
}
masg[1100];
intf[1100];
intmain()
{
intx,y;
inti=1;
while(scanf("%d%d",&x,&y)!=EOF)
{
g[i].weigt=x;
g[i].sped=y;
g[i].key=i;
i++;
}
memset(f,0,sizeof(f));
sort(g+1,g+i,cmp);
f[i-1]=1;
for(intj=i-2;j>=0;j--)
{
intmax=0;
for(intk=j+1;k<i;k++)
{
if(g[k].sped<g[j].sped&&g[k].weigt>g[j].weigt&&max<f[k])
{
max=f[k];
}
}
f[j]=max+1;
}
intmax1=0;
intpi;
for(intj=1;j<i;j++)
{
if(f[j]>max1)
{
pi=j;
max1=f[j];
}
}
printf("%d\n",max1);
for(intj=pi;j<i;j++)
{
if(f[j]==max1)
{
printf("%d\n",g[j].key);
max1--;
}
if(max1==0)
break;
}
return0;

}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: