您的位置:首页 > 其它

UVa1346 - Songs

2013-03-21 17:08 232 查看
JohnDoeisafamousDJand,therefore,hastheproblemofoptimizingtheplacementofsongsonhistapes.ForagiventapeandforeachsongonthattapeJohnknowsthelengthofthesongandthefrequencyofplayingthatsong.Hisproblemistorecordthesongsonthetapeinanorderthatminimizestheexpectedaccesstime.IfthesongsarerecordedintheorderS(s1),...,Ss(n)onthetapethenthefunctionthatmustbeminimizedis

ViewCode

#include<stdio.h>
#defineMAXN0x1001B
typedefstruct
{
intid;
intlen;
doublerate;
}NODE;
NODEa[MAXN];
voidqsort(intl,intr)
{
inti,j;
NODEmid;
NODEtemp;
i=l;
j=r;
mid=a[(l+r)/2];
while(i<=j)
{
while(a[i].len*mid.rate<mid.len*a[i].rate)i++;
while(a[j].len*mid.rate>mid.len*a[j].rate)j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;

}
}
if(i<r)qsort(i,r);
if(j>l)qsort(l,j);
}
intn,m;
intmain(void)
{
inti;
while(scanf("%d",&n)==1)
{
for(i=0;i<n;i++)
scanf("%d%d%lf",&a[i].id,&a[i].len,&a[i].rate);
scanf("%d",&m);
qsort(0,n-1);
printf("%d\n",a[m-1].id);
}
return0;
}



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