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;
}
相关文章推荐
- HDU 1160 FatMouse's Speed(DP)
- HDU 1160 FatMouse's Speed (dp, 最长子序列)
- HDU 1160 FatMouse's Speed(DP)
- hdu 1160 FatMouse's Speed (dp---最长有序子序列)
- HDU 1160 FatMouse's Speed(DP)
- HDU 1160 FatMouse's Speed--dP--(元素1递增元素2递降的最长子序列)
- hdu 1160 FatMouse's Speed 题解(简单dp)
- HDU1160——FatMouse's Speed(DP入门)
- hdu 1160 FatMouse's Speed(水DP)
- HDU 1160 FatMouse's Speed(dp)
- 【HDU】 1160 FatMouse's Speed (DP)
- zju 1108||hdu 1160 FatMouse's Speed(dp,最长单调序列)
- ZOJ 1108 FatMouse's Speed (HDU 1160) DP
- hdu 1160(FatMouse's Speed)(dp)
- hdu 1160 FatMouse's Speed 简单dp 题解
- HDU---1160-FatMouse's Speed (DP)
- hdu 1160 FatMouse's Speed(最大上升子序列dp)
- hdu 1160 FatMouse's Speed(DP)
- HDU 1160 FatMouse's Speed (最长有序的上升子序列)
- HDU 1160 FatMouse's Speed